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>
9 #include <asm/pmac_feature.h>
10 #include <asm/pmac_pfunc.h>
12 #define DBG(fmt...) printk(fmt)
14 static irqreturn_t macio_gpio_irq(int irq, void *data, struct pt_regs *regs)
21 static int macio_do_gpio_irq_enable(struct pmf_function *func)
23 if (func->node->n_intrs < 1)
26 return request_irq(func->node->intrs[0].line, macio_gpio_irq, 0,
27 func->node->name, func);
30 static int macio_do_gpio_irq_disable(struct pmf_function *func)
32 if (func->node->n_intrs < 1)
35 free_irq(func->node->intrs[0].line, func);
39 static int macio_do_gpio_write(PMF_STD_ARGS, u8 value, u8 mask)
41 u8 __iomem *addr = (u8 __iomem *)func->driver_data;
46 if (args && args->count && !args->u[0].v)
50 spin_lock_irqsave(&feature_lock, flags);
52 tmp = (tmp & ~mask) | (value & mask);
53 DBG("Do write 0x%02x to GPIO %s (%p)\n",
54 tmp, func->node->full_name, addr);
56 spin_unlock_irqrestore(&feature_lock, flags);
61 static int macio_do_gpio_read(PMF_STD_ARGS, u8 mask, int rshift, u8 xor)
63 u8 __iomem *addr = (u8 __iomem *)func->driver_data;
66 /* Check if we have room for reply */
67 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
71 *args->u[0].p = ((value & mask) >> rshift) ^ xor;
76 static int macio_do_delay(PMF_STD_ARGS, u32 duration)
78 /* assume we can sleep ! */
79 msleep((duration + 999) / 1000);
83 static struct pmf_handlers macio_gpio_handlers = {
84 .irq_enable = macio_do_gpio_irq_enable,
85 .irq_disable = macio_do_gpio_irq_disable,
86 .write_gpio = macio_do_gpio_write,
87 .read_gpio = macio_do_gpio_read,
88 .delay = macio_do_delay,
91 static void macio_gpio_init_one(struct macio_chip *macio)
93 struct device_node *gparent, *gp;
96 * Find the "gpio" parent node
100 (gparent = of_get_next_child(macio->of_node, gparent)) != NULL;)
101 if (strcmp(gparent->name, "gpio") == 0)
106 DBG("Installing GPIO functions for macio %s\n",
107 macio->of_node->full_name);
110 * Ok, got one, we dont need anything special to track them down, so
111 * we just create them all
113 for (gp = NULL; (gp = of_get_next_child(gparent, gp)) != NULL;) {
114 u32 *reg = (u32 *)get_property(gp, "reg", NULL);
115 unsigned long offset;
119 /* Deal with old style device-tree. We can safely hard code the
120 * offset for now too even if it's a bit gross ...
124 offset += (unsigned long)macio->base;
125 pmf_register_driver(gp, &macio_gpio_handlers, (void *)offset);
128 DBG("Calling initial GPIO functions for macio %s\n",
129 macio->of_node->full_name);
131 /* And now we run all the init ones */
132 for (gp = NULL; (gp = of_get_next_child(gparent, gp)) != NULL;)
133 pmf_do_functions(gp, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
135 /* Note: We do not at this point implement the "at sleep" or "at wake"
136 * functions. I yet to find any for GPIOs anyway
140 static int macio_do_write_reg32(PMF_STD_ARGS, u32 offset, u32 value, u32 mask)
142 struct macio_chip *macio = func->driver_data;
145 spin_lock_irqsave(&feature_lock, flags);
146 MACIO_OUT32(offset, (MACIO_IN32(offset) & ~mask) | (value & mask));
147 spin_unlock_irqrestore(&feature_lock, flags);
151 static int macio_do_read_reg32(PMF_STD_ARGS, u32 offset)
153 struct macio_chip *macio = func->driver_data;
155 /* Check if we have room for reply */
156 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
159 *args->u[0].p = MACIO_IN32(offset);
163 static int macio_do_write_reg8(PMF_STD_ARGS, u32 offset, u8 value, u8 mask)
165 struct macio_chip *macio = func->driver_data;
168 spin_lock_irqsave(&feature_lock, flags);
169 MACIO_OUT8(offset, (MACIO_IN8(offset) & ~mask) | (value & mask));
170 spin_unlock_irqrestore(&feature_lock, flags);
174 static int macio_do_read_reg8(PMF_STD_ARGS, u32 offset)
176 struct macio_chip *macio = func->driver_data;
178 /* Check if we have room for reply */
179 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
182 *((u8 *)(args->u[0].p)) = MACIO_IN8(offset);
186 static int macio_do_read_reg32_msrx(PMF_STD_ARGS, u32 offset, u32 mask,
189 struct macio_chip *macio = func->driver_data;
191 /* Check if we have room for reply */
192 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
195 *args->u[0].p = ((MACIO_IN32(offset) & mask) >> shift) ^ xor;
199 static int macio_do_read_reg8_msrx(PMF_STD_ARGS, u32 offset, u32 mask,
202 struct macio_chip *macio = func->driver_data;
204 /* Check if we have room for reply */
205 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
208 *((u8 *)(args->u[0].p)) = ((MACIO_IN8(offset) & mask) >> shift) ^ xor;
212 static int macio_do_write_reg32_slm(PMF_STD_ARGS, u32 offset, u32 shift,
215 struct macio_chip *macio = func->driver_data;
220 if (args == NULL || args->count == 0)
223 spin_lock_irqsave(&feature_lock, flags);
224 tmp = MACIO_IN32(offset);
225 val = args->u[0].v << shift;
226 tmp = (tmp & ~mask) | (val & mask);
227 MACIO_OUT32(offset, tmp);
228 spin_unlock_irqrestore(&feature_lock, flags);
232 static int macio_do_write_reg8_slm(PMF_STD_ARGS, u32 offset, u32 shift,
235 struct macio_chip *macio = func->driver_data;
240 if (args == NULL || args->count == 0)
243 spin_lock_irqsave(&feature_lock, flags);
244 tmp = MACIO_IN8(offset);
245 val = args->u[0].v << shift;
246 tmp = (tmp & ~mask) | (val & mask);
247 MACIO_OUT8(offset, tmp);
248 spin_unlock_irqrestore(&feature_lock, flags);
252 static struct pmf_handlers macio_mmio_handlers = {
253 .write_reg32 = macio_do_write_reg32,
254 .read_reg32 = macio_do_read_reg32,
255 .write_reg8 = macio_do_write_reg8,
256 .read_reg32 = macio_do_read_reg8,
257 .read_reg32_msrx = macio_do_read_reg32_msrx,
258 .read_reg8_msrx = macio_do_read_reg8_msrx,
259 .write_reg32_slm = macio_do_write_reg32_slm,
260 .write_reg8_slm = macio_do_write_reg8_slm,
261 .delay = macio_do_delay,
264 static void macio_mmio_init_one(struct macio_chip *macio)
266 DBG("Installing MMIO functions for macio %s\n",
267 macio->of_node->full_name);
269 pmf_register_driver(macio->of_node, &macio_mmio_handlers, macio);
272 static struct device_node *unin_hwclock;
274 static int unin_do_write_reg32(PMF_STD_ARGS, u32 offset, u32 value, u32 mask)
278 spin_lock_irqsave(&feature_lock, flags);
279 /* This is fairly bogus in darwin, but it should work for our needs
280 * implemeted that way:
282 UN_OUT(offset, (UN_IN(offset) & ~mask) | (value & mask));
283 spin_unlock_irqrestore(&feature_lock, flags);
288 static struct pmf_handlers unin_mmio_handlers = {
289 .write_reg32 = unin_do_write_reg32,
290 .delay = macio_do_delay,
293 static void uninorth_install_pfunc(void)
295 struct device_node *np;
297 DBG("Installing functions for UniN %s\n",
298 uninorth_node->full_name);
301 * Install handlers for the bridge itself
303 pmf_register_driver(uninorth_node, &unin_mmio_handlers, NULL);
304 pmf_do_functions(uninorth_node, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
308 * Install handlers for the hwclock child if any
310 for (np = NULL; (np = of_get_next_child(uninorth_node, np)) != NULL;)
311 if (strcmp(np->name, "hw-clock") == 0) {
316 DBG("Installing functions for UniN clock %s\n",
317 unin_hwclock->full_name);
318 pmf_register_driver(unin_hwclock, &unin_mmio_handlers, NULL);
319 pmf_do_functions(unin_hwclock, NULL, 0, PMF_FLAGS_ON_INIT,
324 /* We export this as the SMP code might init us early */
325 int __init pmac_pfunc_base_install(void)
327 static int pfbase_inited;
335 DBG("Installing base platform functions...\n");
338 * Locate mac-io chips and install handlers
340 for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
341 if (macio_chips[i].of_node) {
342 macio_mmio_init_one(&macio_chips[i]);
343 macio_gpio_init_one(&macio_chips[i]);
348 * Install handlers for northbridge and direct mapped hwclock
349 * if any. We do not implement the config space access callback
350 * which is only ever used for functions that we do not call in
351 * the current driver (enabling/disabling cells in U2, mostly used
352 * to restore the PCI settings, we do that differently)
354 if (uninorth_node && uninorth_base)
355 uninorth_install_pfunc();
357 DBG("All base functions installed\n");
362 arch_initcall(pmac_pfunc_base_install);
366 /* Those can be called by pmac_feature. Ultimately, I should use a sysdev
367 * or a device, but for now, that's good enough until I sort out some
368 * ordering issues. Also, we do not bother with GPIOs, as so far I yet have
369 * to see a case where a GPIO function has the on-suspend or on-resume bit
371 void pmac_pfunc_base_suspend(void)
375 for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
376 if (macio_chips[i].of_node)
377 pmf_do_functions(macio_chips[i].of_node, NULL, 0,
378 PMF_FLAGS_ON_SLEEP, NULL);
381 pmf_do_functions(uninorth_node, NULL, 0,
382 PMF_FLAGS_ON_SLEEP, NULL);
384 pmf_do_functions(unin_hwclock, NULL, 0,
385 PMF_FLAGS_ON_SLEEP, NULL);
388 void pmac_pfunc_base_resume(void)
393 pmf_do_functions(unin_hwclock, NULL, 0,
394 PMF_FLAGS_ON_WAKE, NULL);
396 pmf_do_functions(uninorth_node, NULL, 0,
397 PMF_FLAGS_ON_WAKE, NULL);
398 for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
399 if (macio_chips[i].of_node)
400 pmf_do_functions(macio_chips[i].of_node, NULL, 0,
401 PMF_FLAGS_ON_WAKE, NULL);
405 #endif /* CONFIG_PM */