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
)
25 static int macio_do_gpio_irq_enable(struct pmf_function
*func
)
27 unsigned int irq
= irq_of_parse_and_map(func
->node
, 0);
30 return request_irq(irq
, macio_gpio_irq
, 0, func
->node
->name
, func
);
33 static int macio_do_gpio_irq_disable(struct pmf_function
*func
)
35 unsigned int irq
= irq_of_parse_and_map(func
->node
, 0);
42 static int macio_do_gpio_write(PMF_STD_ARGS
, u8 value
, u8 mask
)
44 u8 __iomem
*addr
= (u8 __iomem
*)func
->driver_data
;
49 if (args
&& args
->count
&& !args
->u
[0].v
)
53 spin_lock_irqsave(&feature_lock
, flags
);
55 tmp
= (tmp
& ~mask
) | (value
& mask
);
56 DBG("Do write 0x%02x to GPIO %s (%p)\n",
57 tmp
, func
->node
->full_name
, addr
);
59 spin_unlock_irqrestore(&feature_lock
, flags
);
64 static int macio_do_gpio_read(PMF_STD_ARGS
, u8 mask
, int rshift
, u8
xor)
66 u8 __iomem
*addr
= (u8 __iomem
*)func
->driver_data
;
69 /* Check if we have room for reply */
70 if (args
== NULL
|| args
->count
== 0 || args
->u
[0].p
== NULL
)
74 *args
->u
[0].p
= ((value
& mask
) >> rshift
) ^ xor;
79 static int macio_do_delay(PMF_STD_ARGS
, u32 duration
)
81 /* assume we can sleep ! */
82 msleep((duration
+ 999) / 1000);
86 static struct pmf_handlers macio_gpio_handlers
= {
87 .irq_enable
= macio_do_gpio_irq_enable
,
88 .irq_disable
= macio_do_gpio_irq_disable
,
89 .write_gpio
= macio_do_gpio_write
,
90 .read_gpio
= macio_do_gpio_read
,
91 .delay
= macio_do_delay
,
94 static void macio_gpio_init_one(struct macio_chip
*macio
)
96 struct device_node
*gparent
, *gp
;
99 * Find the "gpio" parent node
103 (gparent
= of_get_next_child(macio
->of_node
, gparent
)) != NULL
;)
104 if (strcmp(gparent
->name
, "gpio") == 0)
109 DBG("Installing GPIO functions for macio %s\n",
110 macio
->of_node
->full_name
);
113 * Ok, got one, we dont need anything special to track them down, so
114 * we just create them all
116 for (gp
= NULL
; (gp
= of_get_next_child(gparent
, gp
)) != NULL
;) {
117 const u32
*reg
= of_get_property(gp
, "reg", NULL
);
118 unsigned long offset
;
122 /* Deal with old style device-tree. We can safely hard code the
123 * offset for now too even if it's a bit gross ...
127 offset
+= (unsigned long)macio
->base
;
128 pmf_register_driver(gp
, &macio_gpio_handlers
, (void *)offset
);
131 DBG("Calling initial GPIO functions for macio %s\n",
132 macio
->of_node
->full_name
);
134 /* And now we run all the init ones */
135 for (gp
= NULL
; (gp
= of_get_next_child(gparent
, gp
)) != NULL
;)
136 pmf_do_functions(gp
, NULL
, 0, PMF_FLAGS_ON_INIT
, NULL
);
138 /* Note: We do not at this point implement the "at sleep" or "at wake"
139 * functions. I yet to find any for GPIOs anyway
143 static int macio_do_write_reg32(PMF_STD_ARGS
, u32 offset
, u32 value
, u32 mask
)
145 struct macio_chip
*macio
= func
->driver_data
;
148 spin_lock_irqsave(&feature_lock
, flags
);
149 MACIO_OUT32(offset
, (MACIO_IN32(offset
) & ~mask
) | (value
& mask
));
150 spin_unlock_irqrestore(&feature_lock
, flags
);
154 static int macio_do_read_reg32(PMF_STD_ARGS
, u32 offset
)
156 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
)
162 *args
->u
[0].p
= MACIO_IN32(offset
);
166 static int macio_do_write_reg8(PMF_STD_ARGS
, u32 offset
, u8 value
, u8 mask
)
168 struct macio_chip
*macio
= func
->driver_data
;
171 spin_lock_irqsave(&feature_lock
, flags
);
172 MACIO_OUT8(offset
, (MACIO_IN8(offset
) & ~mask
) | (value
& mask
));
173 spin_unlock_irqrestore(&feature_lock
, flags
);
177 static int macio_do_read_reg8(PMF_STD_ARGS
, u32 offset
)
179 struct macio_chip
*macio
= func
->driver_data
;
181 /* Check if we have room for reply */
182 if (args
== NULL
|| args
->count
== 0 || args
->u
[0].p
== NULL
)
185 *((u8
*)(args
->u
[0].p
)) = MACIO_IN8(offset
);
189 static int macio_do_read_reg32_msrx(PMF_STD_ARGS
, u32 offset
, u32 mask
,
192 struct macio_chip
*macio
= func
->driver_data
;
194 /* Check if we have room for reply */
195 if (args
== NULL
|| args
->count
== 0 || args
->u
[0].p
== NULL
)
198 *args
->u
[0].p
= ((MACIO_IN32(offset
) & mask
) >> shift
) ^ xor;
202 static int macio_do_read_reg8_msrx(PMF_STD_ARGS
, u32 offset
, u32 mask
,
205 struct macio_chip
*macio
= func
->driver_data
;
207 /* Check if we have room for reply */
208 if (args
== NULL
|| args
->count
== 0 || args
->u
[0].p
== NULL
)
211 *((u8
*)(args
->u
[0].p
)) = ((MACIO_IN8(offset
) & mask
) >> shift
) ^ xor;
215 static int macio_do_write_reg32_slm(PMF_STD_ARGS
, u32 offset
, u32 shift
,
218 struct macio_chip
*macio
= func
->driver_data
;
223 if (args
== NULL
|| args
->count
== 0)
226 spin_lock_irqsave(&feature_lock
, flags
);
227 tmp
= MACIO_IN32(offset
);
228 val
= args
->u
[0].v
<< shift
;
229 tmp
= (tmp
& ~mask
) | (val
& mask
);
230 MACIO_OUT32(offset
, tmp
);
231 spin_unlock_irqrestore(&feature_lock
, flags
);
235 static int macio_do_write_reg8_slm(PMF_STD_ARGS
, u32 offset
, u32 shift
,
238 struct macio_chip
*macio
= func
->driver_data
;
243 if (args
== NULL
|| args
->count
== 0)
246 spin_lock_irqsave(&feature_lock
, flags
);
247 tmp
= MACIO_IN8(offset
);
248 val
= args
->u
[0].v
<< shift
;
249 tmp
= (tmp
& ~mask
) | (val
& mask
);
250 MACIO_OUT8(offset
, tmp
);
251 spin_unlock_irqrestore(&feature_lock
, flags
);
255 static struct pmf_handlers macio_mmio_handlers
= {
256 .write_reg32
= macio_do_write_reg32
,
257 .read_reg32
= macio_do_read_reg32
,
258 .write_reg8
= macio_do_write_reg8
,
259 .read_reg8
= macio_do_read_reg8
,
260 .read_reg32_msrx
= macio_do_read_reg32_msrx
,
261 .read_reg8_msrx
= macio_do_read_reg8_msrx
,
262 .write_reg32_slm
= macio_do_write_reg32_slm
,
263 .write_reg8_slm
= macio_do_write_reg8_slm
,
264 .delay
= macio_do_delay
,
267 static void macio_mmio_init_one(struct macio_chip
*macio
)
269 DBG("Installing MMIO functions for macio %s\n",
270 macio
->of_node
->full_name
);
272 pmf_register_driver(macio
->of_node
, &macio_mmio_handlers
, macio
);
275 static struct device_node
*unin_hwclock
;
277 static int unin_do_write_reg32(PMF_STD_ARGS
, u32 offset
, u32 value
, u32 mask
)
281 spin_lock_irqsave(&feature_lock
, flags
);
282 /* This is fairly bogus in darwin, but it should work for our needs
283 * implemeted that way:
285 UN_OUT(offset
, (UN_IN(offset
) & ~mask
) | (value
& mask
));
286 spin_unlock_irqrestore(&feature_lock
, flags
);
291 static struct pmf_handlers unin_mmio_handlers
= {
292 .write_reg32
= unin_do_write_reg32
,
293 .delay
= macio_do_delay
,
296 static void uninorth_install_pfunc(void)
298 struct device_node
*np
;
300 DBG("Installing functions for UniN %s\n",
301 uninorth_node
->full_name
);
304 * Install handlers for the bridge itself
306 pmf_register_driver(uninorth_node
, &unin_mmio_handlers
, NULL
);
307 pmf_do_functions(uninorth_node
, NULL
, 0, PMF_FLAGS_ON_INIT
, NULL
);
311 * Install handlers for the hwclock child if any
313 for (np
= NULL
; (np
= of_get_next_child(uninorth_node
, np
)) != NULL
;)
314 if (strcmp(np
->name
, "hw-clock") == 0) {
319 DBG("Installing functions for UniN clock %s\n",
320 unin_hwclock
->full_name
);
321 pmf_register_driver(unin_hwclock
, &unin_mmio_handlers
, NULL
);
322 pmf_do_functions(unin_hwclock
, NULL
, 0, PMF_FLAGS_ON_INIT
,
327 /* We export this as the SMP code might init us early */
328 int __init
pmac_pfunc_base_install(void)
330 static int pfbase_inited
;
337 if (!machine_is(powermac
))
340 DBG("Installing base platform functions...\n");
343 * Locate mac-io chips and install handlers
345 for (i
= 0 ; i
< MAX_MACIO_CHIPS
; i
++) {
346 if (macio_chips
[i
].of_node
) {
347 macio_mmio_init_one(&macio_chips
[i
]);
348 macio_gpio_init_one(&macio_chips
[i
]);
353 * Install handlers for northbridge and direct mapped hwclock
354 * if any. We do not implement the config space access callback
355 * which is only ever used for functions that we do not call in
356 * the current driver (enabling/disabling cells in U2, mostly used
357 * to restore the PCI settings, we do that differently)
359 if (uninorth_node
&& uninorth_base
)
360 uninorth_install_pfunc();
362 DBG("All base functions installed\n");
366 machine_arch_initcall(powermac
, pmac_pfunc_base_install
);
370 /* Those can be called by pmac_feature. Ultimately, I should use a sysdev
371 * or a device, but for now, that's good enough until I sort out some
372 * ordering issues. Also, we do not bother with GPIOs, as so far I yet have
373 * to see a case where a GPIO function has the on-suspend or on-resume bit
375 void pmac_pfunc_base_suspend(void)
379 for (i
= 0 ; i
< MAX_MACIO_CHIPS
; i
++) {
380 if (macio_chips
[i
].of_node
)
381 pmf_do_functions(macio_chips
[i
].of_node
, NULL
, 0,
382 PMF_FLAGS_ON_SLEEP
, NULL
);
385 pmf_do_functions(uninorth_node
, NULL
, 0,
386 PMF_FLAGS_ON_SLEEP
, NULL
);
388 pmf_do_functions(unin_hwclock
, NULL
, 0,
389 PMF_FLAGS_ON_SLEEP
, NULL
);
392 void pmac_pfunc_base_resume(void)
397 pmf_do_functions(unin_hwclock
, NULL
, 0,
398 PMF_FLAGS_ON_WAKE
, NULL
);
400 pmf_do_functions(uninorth_node
, NULL
, 0,
401 PMF_FLAGS_ON_WAKE
, NULL
);
402 for (i
= 0 ; i
< MAX_MACIO_CHIPS
; i
++) {
403 if (macio_chips
[i
].of_node
)
404 pmf_do_functions(macio_chips
[i
].of_node
, NULL
, 0,
405 PMF_FLAGS_ON_WAKE
, NULL
);
409 #endif /* CONFIG_PM */