2 * Bus driver for MIPS Common Device Memory Map (CDMM).
4 * Copyright (C) 2014-2015 Imagination Technologies Ltd.
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 #include <linux/atomic.h>
12 #include <linux/err.h>
13 #include <linux/cpu.h>
14 #include <linux/cpumask.h>
16 #include <linux/of_address.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/smp.h>
22 #include <asm/hazards.h>
23 #include <asm/mipsregs.h>
25 /* Access control and status register fields */
26 #define CDMM_ACSR_DEVTYPE_SHIFT 24
27 #define CDMM_ACSR_DEVTYPE (255ul << CDMM_ACSR_DEVTYPE_SHIFT)
28 #define CDMM_ACSR_DEVSIZE_SHIFT 16
29 #define CDMM_ACSR_DEVSIZE (31ul << CDMM_ACSR_DEVSIZE_SHIFT)
30 #define CDMM_ACSR_DEVREV_SHIFT 12
31 #define CDMM_ACSR_DEVREV (15ul << CDMM_ACSR_DEVREV_SHIFT)
32 #define CDMM_ACSR_UW (1ul << 3)
33 #define CDMM_ACSR_UR (1ul << 2)
34 #define CDMM_ACSR_SW (1ul << 1)
35 #define CDMM_ACSR_SR (1ul << 0)
37 /* Each block of device registers is 64 bytes */
38 #define CDMM_DRB_SIZE 64
40 #define to_mips_cdmm_driver(d) container_of(d, struct mips_cdmm_driver, drv)
42 /* Default physical base address */
43 static phys_addr_t mips_cdmm_default_base
;
47 static const struct mips_cdmm_device_id
*
48 mips_cdmm_lookup(const struct mips_cdmm_device_id
*table
,
49 struct mips_cdmm_device
*dev
)
53 for (; table
->type
; ++table
) {
54 ret
= (dev
->type
== table
->type
);
59 return ret
? table
: NULL
;
62 static int mips_cdmm_match(struct device
*dev
, struct device_driver
*drv
)
64 struct mips_cdmm_device
*cdev
= to_mips_cdmm_device(dev
);
65 struct mips_cdmm_driver
*cdrv
= to_mips_cdmm_driver(drv
);
67 return mips_cdmm_lookup(cdrv
->id_table
, cdev
) != NULL
;
70 static int mips_cdmm_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
72 struct mips_cdmm_device
*cdev
= to_mips_cdmm_device(dev
);
75 retval
= add_uevent_var(env
, "CDMM_CPU=%u", cdev
->cpu
);
79 retval
= add_uevent_var(env
, "CDMM_TYPE=0x%02x", cdev
->type
);
83 retval
= add_uevent_var(env
, "CDMM_REV=%u", cdev
->rev
);
87 retval
= add_uevent_var(env
, "MODALIAS=mipscdmm:t%02X", cdev
->type
);
91 /* Device attributes */
93 #define CDMM_ATTR(name, fmt, arg...) \
94 static ssize_t name##_show(struct device *_dev, \
95 struct device_attribute *attr, char *buf) \
97 struct mips_cdmm_device *dev = to_mips_cdmm_device(_dev); \
98 return sprintf(buf, fmt, arg); \
100 static DEVICE_ATTR_RO(name);
102 CDMM_ATTR(cpu
, "%u\n", dev
->cpu
);
103 CDMM_ATTR(type
, "0x%02x\n", dev
->type
);
104 CDMM_ATTR(revision
, "%u\n", dev
->rev
);
105 CDMM_ATTR(modalias
, "mipscdmm:t%02X\n", dev
->type
);
106 CDMM_ATTR(resource
, "\t%016llx\t%016llx\t%016lx\n",
107 (unsigned long long)dev
->res
.start
,
108 (unsigned long long)dev
->res
.end
,
111 static struct attribute
*mips_cdmm_dev_attrs
[] = {
114 &dev_attr_revision
.attr
,
115 &dev_attr_modalias
.attr
,
116 &dev_attr_resource
.attr
,
119 ATTRIBUTE_GROUPS(mips_cdmm_dev
);
121 struct bus_type mips_cdmm_bustype
= {
123 .dev_groups
= mips_cdmm_dev_groups
,
124 .match
= mips_cdmm_match
,
125 .uevent
= mips_cdmm_uevent
,
127 EXPORT_SYMBOL_GPL(mips_cdmm_bustype
);
130 * Standard driver callback helpers.
132 * All the CDMM driver callbacks need to be executed on the appropriate CPU from
133 * workqueues. For the standard driver callbacks we need a work function
134 * (mips_cdmm_{void,int}_work()) to do the actual call from the right CPU, and a
135 * wrapper function (generated with BUILD_PERCPU_HELPER) to arrange for the work
136 * function to be called on that CPU.
140 * struct mips_cdmm_work_dev - Data for per-device call work.
141 * @fn: CDMM driver callback function to call for the device.
142 * @dev: CDMM device to pass to @fn.
144 struct mips_cdmm_work_dev
{
146 struct mips_cdmm_device
*dev
;
150 * mips_cdmm_void_work() - Call a void returning CDMM driver callback.
151 * @data: struct mips_cdmm_work_dev pointer.
153 * A work_on_cpu() callback function to call an arbitrary CDMM driver callback
154 * function which doesn't return a value.
156 static long mips_cdmm_void_work(void *data
)
158 struct mips_cdmm_work_dev
*work
= data
;
159 void (*fn
)(struct mips_cdmm_device
*) = work
->fn
;
166 * mips_cdmm_int_work() - Call an int returning CDMM driver callback.
167 * @data: struct mips_cdmm_work_dev pointer.
169 * A work_on_cpu() callback function to call an arbitrary CDMM driver callback
170 * function which returns an int.
172 static long mips_cdmm_int_work(void *data
)
174 struct mips_cdmm_work_dev
*work
= data
;
175 int (*fn
)(struct mips_cdmm_device
*) = work
->fn
;
177 return fn(work
->dev
);
180 #define _BUILD_RET_void
181 #define _BUILD_RET_int return
184 * BUILD_PERCPU_HELPER() - Helper to call a CDMM driver callback on right CPU.
185 * @_ret: Return type (void or int).
186 * @_name: Name of CDMM driver callback function.
188 * Generates a specific device callback function to call a CDMM driver callback
189 * function on the appropriate CPU for the device, and if applicable return the
192 #define BUILD_PERCPU_HELPER(_ret, _name) \
193 static _ret mips_cdmm_##_name(struct device *dev) \
195 struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev); \
196 struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(dev->driver); \
197 struct mips_cdmm_work_dev work = { \
202 _BUILD_RET_##_ret work_on_cpu(cdev->cpu, \
203 mips_cdmm_##_ret##_work, &work); \
206 /* Driver callback functions */
207 BUILD_PERCPU_HELPER(int, probe
) /* int mips_cdmm_probe(struct device) */
208 BUILD_PERCPU_HELPER(int, remove
) /* int mips_cdmm_remove(struct device) */
209 BUILD_PERCPU_HELPER(void, shutdown
) /* void mips_cdmm_shutdown(struct device) */
212 /* Driver registration */
215 * mips_cdmm_driver_register() - Register a CDMM driver.
216 * @drv: CDMM driver information.
218 * Register a CDMM driver with the CDMM subsystem. The driver will be informed
219 * of matching devices which are discovered.
221 * Returns: 0 on success.
223 int mips_cdmm_driver_register(struct mips_cdmm_driver
*drv
)
225 drv
->drv
.bus
= &mips_cdmm_bustype
;
228 drv
->drv
.probe
= mips_cdmm_probe
;
230 drv
->drv
.remove
= mips_cdmm_remove
;
232 drv
->drv
.shutdown
= mips_cdmm_shutdown
;
234 return driver_register(&drv
->drv
);
236 EXPORT_SYMBOL_GPL(mips_cdmm_driver_register
);
239 * mips_cdmm_driver_unregister() - Unregister a CDMM driver.
240 * @drv: CDMM driver information.
242 * Unregister a CDMM driver from the CDMM subsystem.
244 void mips_cdmm_driver_unregister(struct mips_cdmm_driver
*drv
)
246 driver_unregister(&drv
->drv
);
248 EXPORT_SYMBOL_GPL(mips_cdmm_driver_unregister
);
251 /* CDMM initialisation and bus discovery */
254 * struct mips_cdmm_bus - Info about CDMM bus.
255 * @phys: Physical address at which it is mapped.
256 * @regs: Virtual address where registers can be accessed.
257 * @drbs: Total number of DRBs.
258 * @drbs_reserved: Number of DRBs reserved.
259 * @discovered: Whether the devices on the bus have been discovered yet.
260 * @offline: Whether the CDMM bus is going offline (or very early
261 * coming back online), in which case it should be
262 * reconfigured each time.
264 struct mips_cdmm_bus
{
268 unsigned int drbs_reserved
;
273 static struct mips_cdmm_bus mips_cdmm_boot_bus
;
274 static DEFINE_PER_CPU(struct mips_cdmm_bus
*, mips_cdmm_buses
);
275 static atomic_t mips_cdmm_next_id
= ATOMIC_INIT(-1);
278 * mips_cdmm_get_bus() - Get the per-CPU CDMM bus information.
280 * Get information about the per-CPU CDMM bus, if the bus is present.
282 * The caller must prevent migration to another CPU, either by disabling
283 * pre-emption or by running from a pinned kernel thread.
285 * Returns: Pointer to CDMM bus information for the current CPU.
286 * May return ERR_PTR(-errno) in case of error, so check with
289 static struct mips_cdmm_bus
*mips_cdmm_get_bus(void)
291 struct mips_cdmm_bus
*bus
, **bus_p
;
296 return ERR_PTR(-ENODEV
);
298 cpu
= smp_processor_id();
299 /* Avoid early use of per-cpu primitives before initialised */
301 return &mips_cdmm_boot_bus
;
303 /* Get bus pointer */
304 bus_p
= per_cpu_ptr(&mips_cdmm_buses
, cpu
);
305 local_irq_save(flags
);
307 /* Attempt allocation if NULL */
308 if (unlikely(!bus
)) {
309 bus
= kzalloc(sizeof(*bus
), GFP_ATOMIC
);
311 bus
= ERR_PTR(-ENOMEM
);
315 local_irq_restore(flags
);
320 * mips_cdmm_cur_base() - Find current physical base address of CDMM region.
322 * Returns: Physical base address of CDMM region according to cdmmbase CP0
323 * register, or 0 if the CDMM region is disabled.
325 static phys_addr_t
mips_cdmm_cur_base(void)
327 unsigned long cdmmbase
= read_c0_cdmmbase();
329 if (!(cdmmbase
& MIPS_CDMMBASE_EN
))
332 return (cdmmbase
>> MIPS_CDMMBASE_ADDR_SHIFT
)
333 << MIPS_CDMMBASE_ADDR_START
;
337 * mips_cdmm_phys_base() - Choose a physical base address for CDMM region.
339 * Picking a suitable physical address at which to map the CDMM region is
340 * platform specific, so this weak function can be overridden by platform
341 * code to pick a suitable value if none is configured by the bootloader.
342 * By default this method tries to find a CDMM-specific node in the system
343 * dtb. Note that this won't work for early serial console.
345 phys_addr_t __weak
mips_cdmm_phys_base(void)
347 struct device_node
*np
;
351 np
= of_find_compatible_node(NULL
, NULL
, "mti,mips-cdmm");
353 err
= of_address_to_resource(np
, 0, &res
);
362 * mips_cdmm_setup() - Ensure the CDMM bus is initialised and usable.
363 * @bus: Pointer to bus information for current CPU.
364 * IS_ERR(bus) is checked, so no need for caller to check.
366 * The caller must prevent migration to another CPU, either by disabling
367 * pre-emption or by running from a pinned kernel thread.
369 * Returns 0 on success, -errno on failure.
371 static int mips_cdmm_setup(struct mips_cdmm_bus
*bus
)
373 unsigned long cdmmbase
, flags
;
379 local_irq_save(flags
);
380 /* Don't set up bus a second time unless marked offline */
382 /* If CDMM region is still set up, nothing to do */
383 if (bus
->phys
== mips_cdmm_cur_base())
386 * The CDMM region isn't set up as expected, so it needs
387 * reconfiguring, but then we can stop checking it.
389 bus
->offline
= false;
390 } else if (bus
->phys
> 1) {
394 /* If the CDMM region is already configured, inherit that setup */
396 bus
->phys
= mips_cdmm_cur_base();
397 /* Otherwise, ask platform code for suggestions */
399 bus
->phys
= mips_cdmm_phys_base();
400 /* Otherwise, copy what other CPUs have done */
402 bus
->phys
= mips_cdmm_default_base
;
403 /* Otherwise, complain once */
407 * If you hit this, either your bootloader needs to set up the
408 * CDMM on the boot CPU, or else you need to implement
409 * mips_cdmm_phys_base() for your platform (see asm/cdmm.h).
411 pr_err("cdmm%u: Failed to choose a physical base\n",
414 /* Already complained? */
415 if (bus
->phys
== 1) {
419 /* Record our success for other CPUs to copy */
420 mips_cdmm_default_base
= bus
->phys
;
422 pr_debug("cdmm%u: Enabling CDMM region at %pa\n",
423 smp_processor_id(), &bus
->phys
);
426 cdmmbase
= read_c0_cdmmbase();
427 cdmmbase
&= (1ul << MIPS_CDMMBASE_ADDR_SHIFT
) - 1;
428 cdmmbase
|= (bus
->phys
>> MIPS_CDMMBASE_ADDR_START
)
429 << MIPS_CDMMBASE_ADDR_SHIFT
;
430 cdmmbase
|= MIPS_CDMMBASE_EN
;
431 write_c0_cdmmbase(cdmmbase
);
434 bus
->regs
= (void __iomem
*)CKSEG1ADDR(bus
->phys
);
435 bus
->drbs
= 1 + ((cdmmbase
& MIPS_CDMMBASE_SIZE
) >>
436 MIPS_CDMMBASE_SIZE_SHIFT
);
437 bus
->drbs_reserved
= !!(cdmmbase
& MIPS_CDMMBASE_CI
);
440 local_irq_restore(flags
);
445 * mips_cdmm_early_probe() - Minimally probe for a specific device on CDMM.
446 * @dev_type: CDMM type code to look for.
448 * Minimally configure the in-CPU Common Device Memory Map (CDMM) and look for a
449 * specific device. This can be used to find a device very early in boot for
450 * example to configure an early FDC console device.
452 * The caller must prevent migration to another CPU, either by disabling
453 * pre-emption or by running from a pinned kernel thread.
455 * Returns: MMIO pointer to device memory. The caller can read the ACSR
456 * register to find more information about the device (such as the
457 * version number or the number of blocks).
458 * May return IOMEM_ERR_PTR(-errno) in case of error, so check with
461 void __iomem
*mips_cdmm_early_probe(unsigned int dev_type
)
463 struct mips_cdmm_bus
*bus
;
466 unsigned int drb
, type
, size
;
469 if (WARN_ON(!dev_type
))
470 return IOMEM_ERR_PTR(-ENODEV
);
472 bus
= mips_cdmm_get_bus();
473 err
= mips_cdmm_setup(bus
);
475 return IOMEM_ERR_PTR(err
);
477 /* Skip the first block if it's reserved for more registers */
478 drb
= bus
->drbs_reserved
;
481 /* Look for a specific device type */
482 for (; drb
< bus
->drbs
; drb
+= size
+ 1) {
483 acsr
= __raw_readl(cdmm
+ drb
* CDMM_DRB_SIZE
);
484 type
= (acsr
& CDMM_ACSR_DEVTYPE
) >> CDMM_ACSR_DEVTYPE_SHIFT
;
485 if (type
== dev_type
)
486 return cdmm
+ drb
* CDMM_DRB_SIZE
;
487 size
= (acsr
& CDMM_ACSR_DEVSIZE
) >> CDMM_ACSR_DEVSIZE_SHIFT
;
490 return IOMEM_ERR_PTR(-ENODEV
);
492 EXPORT_SYMBOL_GPL(mips_cdmm_early_probe
);
495 * mips_cdmm_release() - Release a removed CDMM device.
496 * @dev: Device object
498 * Clean up the struct mips_cdmm_device for an unused CDMM device. This is
499 * called automatically by the driver core when a device is removed.
501 static void mips_cdmm_release(struct device
*dev
)
503 struct mips_cdmm_device
*cdev
= to_mips_cdmm_device(dev
);
509 * mips_cdmm_bus_discover() - Discover the devices on the CDMM bus.
510 * @bus: CDMM bus information, must already be set up.
512 static void mips_cdmm_bus_discover(struct mips_cdmm_bus
*bus
)
516 unsigned int drb
, type
, size
, rev
;
517 struct mips_cdmm_device
*dev
;
518 unsigned int cpu
= smp_processor_id();
522 /* Skip the first block if it's reserved for more registers */
523 drb
= bus
->drbs_reserved
;
526 /* Discover devices */
527 bus
->discovered
= true;
528 pr_info("cdmm%u discovery (%u blocks)\n", cpu
, bus
->drbs
);
529 for (; drb
< bus
->drbs
; drb
+= size
+ 1) {
530 acsr
= __raw_readl(cdmm
+ drb
* CDMM_DRB_SIZE
);
531 type
= (acsr
& CDMM_ACSR_DEVTYPE
) >> CDMM_ACSR_DEVTYPE_SHIFT
;
532 size
= (acsr
& CDMM_ACSR_DEVSIZE
) >> CDMM_ACSR_DEVSIZE_SHIFT
;
533 rev
= (acsr
& CDMM_ACSR_DEVREV
) >> CDMM_ACSR_DEVREV_SHIFT
;
538 pr_info("cdmm%u-%u: @%u (%#x..%#x), type 0x%02x, rev %u\n",
539 cpu
, id
, drb
, drb
* CDMM_DRB_SIZE
,
540 (drb
+ size
+ 1) * CDMM_DRB_SIZE
- 1,
543 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
548 dev
->res
.start
= bus
->phys
+ drb
* CDMM_DRB_SIZE
;
549 dev
->res
.end
= bus
->phys
+
550 (drb
+ size
+ 1) * CDMM_DRB_SIZE
- 1;
551 dev
->res
.flags
= IORESOURCE_MEM
;
554 dev
->dev
.parent
= get_cpu_device(cpu
);
555 dev
->dev
.bus
= &mips_cdmm_bustype
;
556 dev
->dev
.id
= atomic_inc_return(&mips_cdmm_next_id
);
557 dev
->dev
.release
= mips_cdmm_release
;
559 dev_set_name(&dev
->dev
, "cdmm%u-%u", cpu
, id
);
561 ret
= device_register(&dev
->dev
);
563 put_device(&dev
->dev
);
569 * CPU hotplug and initialisation
571 * All the CDMM driver callbacks need to be executed on the appropriate CPU from
572 * workqueues. For the CPU callbacks, they need to be called for all devices on
573 * that CPU, so the work function calls bus_for_each_dev, using a helper
574 * (generated with BUILD_PERDEV_HELPER) to call the driver callback if the
575 * device's CPU matches.
579 * BUILD_PERDEV_HELPER() - Helper to call a CDMM driver callback if CPU matches.
580 * @_name: Name of CDMM driver callback function.
582 * Generates a bus_for_each_dev callback function to call a specific CDMM driver
583 * callback function for the device if the device's CPU matches that pointed to
584 * by the data argument.
586 * This is used for informing drivers for all devices on a given CPU of some
587 * event (such as the CPU going online/offline).
589 * It is expected to already be called from the appropriate CPU.
591 #define BUILD_PERDEV_HELPER(_name) \
592 static int mips_cdmm_##_name##_helper(struct device *dev, void *data) \
594 struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev); \
595 struct mips_cdmm_driver *cdrv; \
596 unsigned int cpu = *(unsigned int *)data; \
598 if (cdev->cpu != cpu || !dev->driver) \
601 cdrv = to_mips_cdmm_driver(dev->driver); \
604 return cdrv->_name(cdev); \
607 /* bus_for_each_dev callback helper functions */
608 BUILD_PERDEV_HELPER(cpu_down
) /* int mips_cdmm_cpu_down_helper(...) */
609 BUILD_PERDEV_HELPER(cpu_up
) /* int mips_cdmm_cpu_up_helper(...) */
612 * mips_cdmm_cpu_down_prep() - Callback for CPUHP DOWN_PREP:
613 * Tear down the CDMM bus.
614 * @cpu: unsigned int CPU number.
616 * This function is executed on the hotplugged CPU and calls the CDMM
617 * driver cpu_down callback for all devices on that CPU.
619 static int mips_cdmm_cpu_down_prep(unsigned int cpu
)
621 struct mips_cdmm_bus
*bus
;
624 /* Inform all the devices on the bus */
625 ret
= bus_for_each_dev(&mips_cdmm_bustype
, NULL
, &cpu
,
626 mips_cdmm_cpu_down_helper
);
629 * While bus is offline, each use of it should reconfigure it just in
630 * case it is first use when coming back online again.
632 bus
= mips_cdmm_get_bus();
640 * mips_cdmm_cpu_online() - Callback for CPUHP ONLINE: Bring up the CDMM bus.
641 * @cpu: unsigned int CPU number.
643 * This work_on_cpu callback function is executed on a given CPU to discover
644 * CDMM devices on that CPU, or to call the CDMM driver cpu_up callback for all
645 * devices already discovered on that CPU.
647 * It is used as work_on_cpu callback function during
648 * initialisation. When CPUs are brought online the function is
649 * invoked directly on the hotplugged CPU.
651 static int mips_cdmm_cpu_online(unsigned int cpu
)
653 struct mips_cdmm_bus
*bus
;
656 bus
= mips_cdmm_get_bus();
657 ret
= mips_cdmm_setup(bus
);
661 /* Bus now set up, so we can drop the offline flag if still set */
662 bus
->offline
= false;
664 if (!bus
->discovered
)
665 mips_cdmm_bus_discover(bus
);
667 /* Inform all the devices on the bus */
668 ret
= bus_for_each_dev(&mips_cdmm_bustype
, NULL
, &cpu
,
669 mips_cdmm_cpu_up_helper
);
675 * mips_cdmm_init() - Initialise CDMM bus.
677 * Initialise CDMM bus, discover CDMM devices for online CPUs, and arrange for
678 * hotplug notifications so the CDMM drivers can be kept up to date.
680 static int __init
mips_cdmm_init(void)
684 /* Register the bus */
685 ret
= bus_register(&mips_cdmm_bustype
);
689 /* We want to be notified about new CPUs */
690 ret
= cpuhp_setup_state(CPUHP_AP_ONLINE_DYN
, "bus/cdmm:online",
691 mips_cdmm_cpu_online
, mips_cdmm_cpu_down_prep
);
693 pr_warn("cdmm: Failed to register CPU notifier\n");
697 subsys_initcall(mips_cdmm_init
);