pfunc_base.c 10 KB

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