pfunc_base.c 10 KB

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