pfunc_base.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/init.h>
  4. #include <linux/delay.h>
  5. #include <linux/kernel.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/of_irq.h>
  9. #include <asm/pmac_feature.h>
  10. #include <asm/pmac_pfunc.h>
  11. #undef DEBUG
  12. #ifdef DEBUG
  13. #define DBG(fmt...) printk(fmt)
  14. #else
  15. #define DBG(fmt...)
  16. #endif
  17. static irqreturn_t macio_gpio_irq(int irq, void *data)
  18. {
  19. pmf_do_irq(data);
  20. return IRQ_HANDLED;
  21. }
  22. static int macio_do_gpio_irq_enable(struct pmf_function *func)
  23. {
  24. unsigned int irq = irq_of_parse_and_map(func->node, 0);
  25. if (!irq)
  26. return -EINVAL;
  27. return request_irq(irq, macio_gpio_irq, 0, func->node->name, func);
  28. }
  29. static int macio_do_gpio_irq_disable(struct pmf_function *func)
  30. {
  31. unsigned int irq = irq_of_parse_and_map(func->node, 0);
  32. if (!irq)
  33. return -EINVAL;
  34. free_irq(irq, func);
  35. return 0;
  36. }
  37. static int macio_do_gpio_write(PMF_STD_ARGS, u8 value, u8 mask)
  38. {
  39. u8 __iomem *addr = (u8 __iomem *)func->driver_data;
  40. unsigned long flags;
  41. u8 tmp;
  42. /* Check polarity */
  43. if (args && args->count && !args->u[0].v)
  44. value = ~value;
  45. /* Toggle the GPIO */
  46. raw_spin_lock_irqsave(&feature_lock, flags);
  47. tmp = readb(addr);
  48. tmp = (tmp & ~mask) | (value & mask);
  49. DBG("Do write 0x%02x to GPIO %pOF (%p)\n",
  50. tmp, func->node, addr);
  51. writeb(tmp, addr);
  52. raw_spin_unlock_irqrestore(&feature_lock, flags);
  53. return 0;
  54. }
  55. static int macio_do_gpio_read(PMF_STD_ARGS, u8 mask, int rshift, u8 xor)
  56. {
  57. u8 __iomem *addr = (u8 __iomem *)func->driver_data;
  58. u32 value;
  59. /* Check if we have room for reply */
  60. if (args == NULL || args->count == 0 || args->u[0].p == NULL)
  61. return -EINVAL;
  62. value = readb(addr);
  63. *args->u[0].p = ((value & mask) >> rshift) ^ xor;
  64. return 0;
  65. }
  66. static int macio_do_delay(PMF_STD_ARGS, u32 duration)
  67. {
  68. /* assume we can sleep ! */
  69. msleep((duration + 999) / 1000);
  70. return 0;
  71. }
  72. static struct pmf_handlers macio_gpio_handlers = {
  73. .irq_enable = macio_do_gpio_irq_enable,
  74. .irq_disable = macio_do_gpio_irq_disable,
  75. .write_gpio = macio_do_gpio_write,
  76. .read_gpio = macio_do_gpio_read,
  77. .delay = macio_do_delay,
  78. };
  79. static void macio_gpio_init_one(struct macio_chip *macio)
  80. {
  81. struct device_node *gparent, *gp;
  82. /*
  83. * Find the "gpio" parent node
  84. */
  85. for (gparent = NULL;
  86. (gparent = of_get_next_child(macio->of_node, gparent)) != NULL;)
  87. if (strcmp(gparent->name, "gpio") == 0)
  88. break;
  89. if (gparent == NULL)
  90. return;
  91. DBG("Installing GPIO functions for macio %pOF\n",
  92. macio->of_node);
  93. /*
  94. * Ok, got one, we dont need anything special to track them down, so
  95. * we just create them all
  96. */
  97. for (gp = NULL; (gp = of_get_next_child(gparent, gp)) != NULL;) {
  98. const u32 *reg = of_get_property(gp, "reg", NULL);
  99. unsigned long offset;
  100. if (reg == NULL)
  101. continue;
  102. offset = *reg;
  103. /* Deal with old style device-tree. We can safely hard code the
  104. * offset for now too even if it's a bit gross ...
  105. */
  106. if (offset < 0x50)
  107. offset += 0x50;
  108. offset += (unsigned long)macio->base;
  109. pmf_register_driver(gp, &macio_gpio_handlers, (void *)offset);
  110. }
  111. DBG("Calling initial GPIO functions for macio %pOF\n",
  112. macio->of_node);
  113. /* And now we run all the init ones */
  114. for (gp = NULL; (gp = of_get_next_child(gparent, gp)) != NULL;)
  115. pmf_do_functions(gp, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
  116. /* Note: We do not at this point implement the "at sleep" or "at wake"
  117. * functions. I yet to find any for GPIOs anyway
  118. */
  119. }
  120. static int macio_do_write_reg32(PMF_STD_ARGS, u32 offset, u32 value, u32 mask)
  121. {
  122. struct macio_chip *macio = func->driver_data;
  123. unsigned long flags;
  124. raw_spin_lock_irqsave(&feature_lock, flags);
  125. MACIO_OUT32(offset, (MACIO_IN32(offset) & ~mask) | (value & mask));
  126. raw_spin_unlock_irqrestore(&feature_lock, flags);
  127. return 0;
  128. }
  129. static int macio_do_read_reg32(PMF_STD_ARGS, u32 offset)
  130. {
  131. struct macio_chip *macio = func->driver_data;
  132. /* Check if we have room for reply */
  133. if (args == NULL || args->count == 0 || args->u[0].p == NULL)
  134. return -EINVAL;
  135. *args->u[0].p = MACIO_IN32(offset);
  136. return 0;
  137. }
  138. static int macio_do_write_reg8(PMF_STD_ARGS, u32 offset, u8 value, u8 mask)
  139. {
  140. struct macio_chip *macio = func->driver_data;
  141. unsigned long flags;
  142. raw_spin_lock_irqsave(&feature_lock, flags);
  143. MACIO_OUT8(offset, (MACIO_IN8(offset) & ~mask) | (value & mask));
  144. raw_spin_unlock_irqrestore(&feature_lock, flags);
  145. return 0;
  146. }
  147. static int macio_do_read_reg8(PMF_STD_ARGS, u32 offset)
  148. {
  149. struct macio_chip *macio = func->driver_data;
  150. /* Check if we have room for reply */
  151. if (args == NULL || args->count == 0 || args->u[0].p == NULL)
  152. return -EINVAL;
  153. *((u8 *)(args->u[0].p)) = MACIO_IN8(offset);
  154. return 0;
  155. }
  156. static int macio_do_read_reg32_msrx(PMF_STD_ARGS, u32 offset, u32 mask,
  157. u32 shift, u32 xor)
  158. {
  159. struct macio_chip *macio = func->driver_data;
  160. /* Check if we have room for reply */
  161. if (args == NULL || args->count == 0 || args->u[0].p == NULL)
  162. return -EINVAL;
  163. *args->u[0].p = ((MACIO_IN32(offset) & mask) >> shift) ^ xor;
  164. return 0;
  165. }
  166. static int macio_do_read_reg8_msrx(PMF_STD_ARGS, u32 offset, u32 mask,
  167. u32 shift, u32 xor)
  168. {
  169. struct macio_chip *macio = func->driver_data;
  170. /* Check if we have room for reply */
  171. if (args == NULL || args->count == 0 || args->u[0].p == NULL)
  172. return -EINVAL;
  173. *((u8 *)(args->u[0].p)) = ((MACIO_IN8(offset) & mask) >> shift) ^ xor;
  174. return 0;
  175. }
  176. static int macio_do_write_reg32_slm(PMF_STD_ARGS, u32 offset, u32 shift,
  177. u32 mask)
  178. {
  179. struct macio_chip *macio = func->driver_data;
  180. unsigned long flags;
  181. u32 tmp, val;
  182. /* Check args */
  183. if (args == NULL || args->count == 0)
  184. return -EINVAL;
  185. raw_spin_lock_irqsave(&feature_lock, flags);
  186. tmp = MACIO_IN32(offset);
  187. val = args->u[0].v << shift;
  188. tmp = (tmp & ~mask) | (val & mask);
  189. MACIO_OUT32(offset, tmp);
  190. raw_spin_unlock_irqrestore(&feature_lock, flags);
  191. return 0;
  192. }
  193. static int macio_do_write_reg8_slm(PMF_STD_ARGS, u32 offset, u32 shift,
  194. u32 mask)
  195. {
  196. struct macio_chip *macio = func->driver_data;
  197. unsigned long flags;
  198. u32 tmp, val;
  199. /* Check args */
  200. if (args == NULL || args->count == 0)
  201. return -EINVAL;
  202. raw_spin_lock_irqsave(&feature_lock, flags);
  203. tmp = MACIO_IN8(offset);
  204. val = args->u[0].v << shift;
  205. tmp = (tmp & ~mask) | (val & mask);
  206. MACIO_OUT8(offset, tmp);
  207. raw_spin_unlock_irqrestore(&feature_lock, flags);
  208. return 0;
  209. }
  210. static struct pmf_handlers macio_mmio_handlers = {
  211. .write_reg32 = macio_do_write_reg32,
  212. .read_reg32 = macio_do_read_reg32,
  213. .write_reg8 = macio_do_write_reg8,
  214. .read_reg8 = macio_do_read_reg8,
  215. .read_reg32_msrx = macio_do_read_reg32_msrx,
  216. .read_reg8_msrx = macio_do_read_reg8_msrx,
  217. .write_reg32_slm = macio_do_write_reg32_slm,
  218. .write_reg8_slm = macio_do_write_reg8_slm,
  219. .delay = macio_do_delay,
  220. };
  221. static void macio_mmio_init_one(struct macio_chip *macio)
  222. {
  223. DBG("Installing MMIO functions for macio %pOF\n",
  224. macio->of_node);
  225. pmf_register_driver(macio->of_node, &macio_mmio_handlers, macio);
  226. }
  227. static struct device_node *unin_hwclock;
  228. static int unin_do_write_reg32(PMF_STD_ARGS, u32 offset, u32 value, u32 mask)
  229. {
  230. unsigned long flags;
  231. raw_spin_lock_irqsave(&feature_lock, flags);
  232. /* This is fairly bogus in darwin, but it should work for our needs
  233. * implemeted that way:
  234. */
  235. UN_OUT(offset, (UN_IN(offset) & ~mask) | (value & mask));
  236. raw_spin_unlock_irqrestore(&feature_lock, flags);
  237. return 0;
  238. }
  239. static struct pmf_handlers unin_mmio_handlers = {
  240. .write_reg32 = unin_do_write_reg32,
  241. .delay = macio_do_delay,
  242. };
  243. static void uninorth_install_pfunc(void)
  244. {
  245. struct device_node *np;
  246. DBG("Installing functions for UniN %pOF\n",
  247. uninorth_node);
  248. /*
  249. * Install handlers for the bridge itself
  250. */
  251. pmf_register_driver(uninorth_node, &unin_mmio_handlers, NULL);
  252. pmf_do_functions(uninorth_node, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
  253. /*
  254. * Install handlers for the hwclock child if any
  255. */
  256. for (np = NULL; (np = of_get_next_child(uninorth_node, np)) != NULL;)
  257. if (strcmp(np->name, "hw-clock") == 0) {
  258. unin_hwclock = np;
  259. break;
  260. }
  261. if (unin_hwclock) {
  262. DBG("Installing functions for UniN clock %pOF\n",
  263. unin_hwclock);
  264. pmf_register_driver(unin_hwclock, &unin_mmio_handlers, NULL);
  265. pmf_do_functions(unin_hwclock, NULL, 0, PMF_FLAGS_ON_INIT,
  266. NULL);
  267. }
  268. }
  269. /* We export this as the SMP code might init us early */
  270. int __init pmac_pfunc_base_install(void)
  271. {
  272. static int pfbase_inited;
  273. int i;
  274. if (pfbase_inited)
  275. return 0;
  276. pfbase_inited = 1;
  277. if (!machine_is(powermac))
  278. return 0;
  279. DBG("Installing base platform functions...\n");
  280. /*
  281. * Locate mac-io chips and install handlers
  282. */
  283. for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
  284. if (macio_chips[i].of_node) {
  285. macio_mmio_init_one(&macio_chips[i]);
  286. macio_gpio_init_one(&macio_chips[i]);
  287. }
  288. }
  289. /*
  290. * Install handlers for northbridge and direct mapped hwclock
  291. * if any. We do not implement the config space access callback
  292. * which is only ever used for functions that we do not call in
  293. * the current driver (enabling/disabling cells in U2, mostly used
  294. * to restore the PCI settings, we do that differently)
  295. */
  296. if (uninorth_node && uninorth_base)
  297. uninorth_install_pfunc();
  298. DBG("All base functions installed\n");
  299. return 0;
  300. }
  301. machine_arch_initcall(powermac, pmac_pfunc_base_install);
  302. #ifdef CONFIG_PM
  303. /* Those can be called by pmac_feature. Ultimately, I should use a sysdev
  304. * or a device, but for now, that's good enough until I sort out some
  305. * ordering issues. Also, we do not bother with GPIOs, as so far I yet have
  306. * to see a case where a GPIO function has the on-suspend or on-resume bit
  307. */
  308. void pmac_pfunc_base_suspend(void)
  309. {
  310. int i;
  311. for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
  312. if (macio_chips[i].of_node)
  313. pmf_do_functions(macio_chips[i].of_node, NULL, 0,
  314. PMF_FLAGS_ON_SLEEP, NULL);
  315. }
  316. if (uninorth_node)
  317. pmf_do_functions(uninorth_node, NULL, 0,
  318. PMF_FLAGS_ON_SLEEP, NULL);
  319. if (unin_hwclock)
  320. pmf_do_functions(unin_hwclock, NULL, 0,
  321. PMF_FLAGS_ON_SLEEP, NULL);
  322. }
  323. void pmac_pfunc_base_resume(void)
  324. {
  325. int i;
  326. if (unin_hwclock)
  327. pmf_do_functions(unin_hwclock, NULL, 0,
  328. PMF_FLAGS_ON_WAKE, NULL);
  329. if (uninorth_node)
  330. pmf_do_functions(uninorth_node, NULL, 0,
  331. PMF_FLAGS_ON_WAKE, NULL);
  332. for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
  333. if (macio_chips[i].of_node)
  334. pmf_do_functions(macio_chips[i].of_node, NULL, 0,
  335. PMF_FLAGS_ON_WAKE, NULL);
  336. }
  337. }
  338. #endif /* CONFIG_PM */