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>
8 #include <asm/pmac_feature.h>
9 #include <asm/pmac_pfunc.h>
13 #define DBG(fmt...) printk(fmt)
18 static irqreturn_t macio_gpio_irq(int irq, void *data, struct pt_regs *regs)
25 static int macio_do_gpio_irq_enable(struct pmf_function *func)
27 if (func->node->n_intrs < 1)
30 return request_irq(func->node->intrs[0].line, macio_gpio_irq, 0,
31 func->node->name, func);
34 static int macio_do_gpio_irq_disable(struct pmf_function *func)
36 if (func->node->n_intrs < 1)
39 free_irq(func->node->intrs[0].line, func);
43 static int macio_do_gpio_write(PMF_STD_ARGS, u8 value, u8 mask)
45 u8 __iomem *addr = (u8 __iomem *)func->driver_data;
50 if (args && args->count && !args->u[0].v)
54 spin_lock_irqsave(&feature_lock, flags);
56 tmp = (tmp & ~mask) | (value & mask);
57 DBG("Do write 0x%02x to GPIO %s (%p)\n",
58 tmp, func->node->full_name, addr);
60 spin_unlock_irqrestore(&feature_lock, flags);
65 static int macio_do_gpio_read(PMF_STD_ARGS, u8 mask, int rshift, u8 xor)
67 u8 __iomem *addr = (u8 __iomem *)func->driver_data;
70 /* Check if we have room for reply */
71 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
75 *args->u[0].p = ((value & mask) >> rshift) ^ xor;
80 static int macio_do_delay(PMF_STD_ARGS, u32 duration)
82 /* assume we can sleep ! */
83 msleep((duration + 999) / 1000);
87 static struct pmf_handlers macio_gpio_handlers = {
88 .irq_enable = macio_do_gpio_irq_enable,
89 .irq_disable = macio_do_gpio_irq_disable,
90 .write_gpio = macio_do_gpio_write,
91 .read_gpio = macio_do_gpio_read,
92 .delay = macio_do_delay,
95 static void macio_gpio_init_one(struct macio_chip *macio)
97 struct device_node *gparent, *gp;
100 * Find the "gpio" parent node
104 (gparent = of_get_next_child(macio->of_node, gparent)) != NULL;)
105 if (strcmp(gparent->name, "gpio") == 0)
110 DBG("Installing GPIO functions for macio %s\n",
111 macio->of_node->full_name);
114 * Ok, got one, we dont need anything special to track them down, so
115 * we just create them all
117 for (gp = NULL; (gp = of_get_next_child(gparent, gp)) != NULL;) {
118 u32 *reg = (u32 *)get_property(gp, "reg", NULL);
119 unsigned long offset;
123 /* Deal with old style device-tree. We can safely hard code the
124 * offset for now too even if it's a bit gross ...
128 offset += (unsigned long)macio->base;
129 pmf_register_driver(gp, &macio_gpio_handlers, (void *)offset);
132 DBG("Calling initial GPIO functions for macio %s\n",
133 macio->of_node->full_name);
135 /* And now we run all the init ones */
136 for (gp = NULL; (gp = of_get_next_child(gparent, gp)) != NULL;)
137 pmf_do_functions(gp, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
139 /* Note: We do not at this point implement the "at sleep" or "at wake"
140 * functions. I yet to find any for GPIOs anyway
144 static int macio_do_write_reg32(PMF_STD_ARGS, u32 offset, u32 value, u32 mask)
146 struct macio_chip *macio = func->driver_data;
149 spin_lock_irqsave(&feature_lock, flags);
150 MACIO_OUT32(offset, (MACIO_IN32(offset) & ~mask) | (value & mask));
151 spin_unlock_irqrestore(&feature_lock, flags);
155 static int macio_do_read_reg32(PMF_STD_ARGS, u32 offset)
157 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)
163 *args->u[0].p = MACIO_IN32(offset);
167 static int macio_do_write_reg8(PMF_STD_ARGS, u32 offset, u8 value, u8 mask)
169 struct macio_chip *macio = func->driver_data;
172 spin_lock_irqsave(&feature_lock, flags);
173 MACIO_OUT8(offset, (MACIO_IN8(offset) & ~mask) | (value & mask));
174 spin_unlock_irqrestore(&feature_lock, flags);
178 static int macio_do_read_reg8(PMF_STD_ARGS, u32 offset)
180 struct macio_chip *macio = func->driver_data;
182 /* Check if we have room for reply */
183 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
186 *((u8 *)(args->u[0].p)) = MACIO_IN8(offset);
190 static int macio_do_read_reg32_msrx(PMF_STD_ARGS, u32 offset, u32 mask,
193 struct macio_chip *macio = func->driver_data;
195 /* Check if we have room for reply */
196 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
199 *args->u[0].p = ((MACIO_IN32(offset) & mask) >> shift) ^ xor;
203 static int macio_do_read_reg8_msrx(PMF_STD_ARGS, u32 offset, u32 mask,
206 struct macio_chip *macio = func->driver_data;
208 /* Check if we have room for reply */
209 if (args == NULL || args->count == 0 || args->u[0].p == NULL)
212 *((u8 *)(args->u[0].p)) = ((MACIO_IN8(offset) & mask) >> shift) ^ xor;
216 static int macio_do_write_reg32_slm(PMF_STD_ARGS, u32 offset, u32 shift,
219 struct macio_chip *macio = func->driver_data;
224 if (args == NULL || args->count == 0)
227 spin_lock_irqsave(&feature_lock, flags);
228 tmp = MACIO_IN32(offset);
229 val = args->u[0].v << shift;
230 tmp = (tmp & ~mask) | (val & mask);
231 MACIO_OUT32(offset, tmp);
232 spin_unlock_irqrestore(&feature_lock, flags);
236 static int macio_do_write_reg8_slm(PMF_STD_ARGS, u32 offset, u32 shift,
239 struct macio_chip *macio = func->driver_data;
244 if (args == NULL || args->count == 0)
247 spin_lock_irqsave(&feature_lock, flags);
248 tmp = MACIO_IN8(offset);
249 val = args->u[0].v << shift;
250 tmp = (tmp & ~mask) | (val & mask);
251 MACIO_OUT8(offset, tmp);
252 spin_unlock_irqrestore(&feature_lock, flags);
256 static struct pmf_handlers macio_mmio_handlers = {
257 .write_reg32 = macio_do_write_reg32,
258 .read_reg32 = macio_do_read_reg32,
259 .write_reg8 = macio_do_write_reg8,
260 .read_reg32 = macio_do_read_reg8,
261 .read_reg32_msrx = macio_do_read_reg32_msrx,
262 .read_reg8_msrx = macio_do_read_reg8_msrx,
263 .write_reg32_slm = macio_do_write_reg32_slm,
264 .write_reg8_slm = macio_do_write_reg8_slm,
265 .delay = macio_do_delay,
268 static void macio_mmio_init_one(struct macio_chip *macio)
270 DBG("Installing MMIO functions for macio %s\n",
271 macio->of_node->full_name);
273 pmf_register_driver(macio->of_node, &macio_mmio_handlers, macio);
276 static struct device_node *unin_hwclock;
278 static int unin_do_write_reg32(PMF_STD_ARGS, u32 offset, u32 value, u32 mask)
282 spin_lock_irqsave(&feature_lock, flags);
283 /* This is fairly bogus in darwin, but it should work for our needs
284 * implemeted that way:
286 UN_OUT(offset, (UN_IN(offset) & ~mask) | (value & mask));
287 spin_unlock_irqrestore(&feature_lock, flags);
292 static struct pmf_handlers unin_mmio_handlers = {
293 .write_reg32 = unin_do_write_reg32,
294 .delay = macio_do_delay,
297 static void uninorth_install_pfunc(void)
299 struct device_node *np;
301 DBG("Installing functions for UniN %s\n",
302 uninorth_node->full_name);
305 * Install handlers for the bridge itself
307 pmf_register_driver(uninorth_node, &unin_mmio_handlers, NULL);
308 pmf_do_functions(uninorth_node, NULL, 0, PMF_FLAGS_ON_INIT, NULL);
312 * Install handlers for the hwclock child if any
314 for (np = NULL; (np = of_get_next_child(uninorth_node, np)) != NULL;)
315 if (strcmp(np->name, "hw-clock") == 0) {
320 DBG("Installing functions for UniN clock %s\n",
321 unin_hwclock->full_name);
322 pmf_register_driver(unin_hwclock, &unin_mmio_handlers, NULL);
323 pmf_do_functions(unin_hwclock, NULL, 0, PMF_FLAGS_ON_INIT,
328 /* We export this as the SMP code might init us early */
329 int __init pmac_pfunc_base_install(void)
331 static int pfbase_inited;
338 if (!machine_is(powermac))
341 DBG("Installing base platform functions...\n");
344 * Locate mac-io chips and install handlers
346 for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
347 if (macio_chips[i].of_node) {
348 macio_mmio_init_one(&macio_chips[i]);
349 macio_gpio_init_one(&macio_chips[i]);
354 * Install handlers for northbridge and direct mapped hwclock
355 * if any. We do not implement the config space access callback
356 * which is only ever used for functions that we do not call in
357 * the current driver (enabling/disabling cells in U2, mostly used
358 * to restore the PCI settings, we do that differently)
360 if (uninorth_node && uninorth_base)
361 uninorth_install_pfunc();
363 DBG("All base functions installed\n");
368 arch_initcall(pmac_pfunc_base_install);
372 /* Those can be called by pmac_feature. Ultimately, I should use a sysdev
373 * or a device, but for now, that's good enough until I sort out some
374 * ordering issues. Also, we do not bother with GPIOs, as so far I yet have
375 * to see a case where a GPIO function has the on-suspend or on-resume bit
377 void pmac_pfunc_base_suspend(void)
381 for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
382 if (macio_chips[i].of_node)
383 pmf_do_functions(macio_chips[i].of_node, NULL, 0,
384 PMF_FLAGS_ON_SLEEP, NULL);
387 pmf_do_functions(uninorth_node, NULL, 0,
388 PMF_FLAGS_ON_SLEEP, NULL);
390 pmf_do_functions(unin_hwclock, NULL, 0,
391 PMF_FLAGS_ON_SLEEP, NULL);
394 void pmac_pfunc_base_resume(void)
399 pmf_do_functions(unin_hwclock, NULL, 0,
400 PMF_FLAGS_ON_WAKE, NULL);
402 pmf_do_functions(uninorth_node, NULL, 0,
403 PMF_FLAGS_ON_WAKE, NULL);
404 for (i = 0 ; i < MAX_MACIO_CHIPS; i++) {
405 if (macio_chips[i].of_node)
406 pmf_do_functions(macio_chips[i].of_node, NULL, 0,
407 PMF_FLAGS_ON_WAKE, NULL);
411 #endif /* CONFIG_PM */