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