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/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/smp.h>
20 #include <asm/hazards.h>
21 #include <asm/mipsregs.h>
23 /* Access control and status register fields */
24 #define CDMM_ACSR_DEVTYPE_SHIFT 24
25 #define CDMM_ACSR_DEVTYPE (255ul << CDMM_ACSR_DEVTYPE_SHIFT)
26 #define CDMM_ACSR_DEVSIZE_SHIFT 16
27 #define CDMM_ACSR_DEVSIZE (31ul << CDMM_ACSR_DEVSIZE_SHIFT)
28 #define CDMM_ACSR_DEVREV_SHIFT 12
29 #define CDMM_ACSR_DEVREV (15ul << CDMM_ACSR_DEVREV_SHIFT)
30 #define CDMM_ACSR_UW (1ul << 3)
31 #define CDMM_ACSR_UR (1ul << 2)
32 #define CDMM_ACSR_SW (1ul << 1)
33 #define CDMM_ACSR_SR (1ul << 0)
35 /* Each block of device registers is 64 bytes */
36 #define CDMM_DRB_SIZE 64
38 #define to_mips_cdmm_driver(d) container_of(d, struct mips_cdmm_driver, drv)
40 /* Default physical base address */
41 static phys_addr_t mips_cdmm_default_base
;
45 static const struct mips_cdmm_device_id
*
46 mips_cdmm_lookup(const struct mips_cdmm_device_id
*table
,
47 struct mips_cdmm_device
*dev
)
51 for (; table
->type
; ++table
) {
52 ret
= (dev
->type
== table
->type
);
57 return ret
? table
: NULL
;
60 static int mips_cdmm_match(struct device
*dev
, struct device_driver
*drv
)
62 struct mips_cdmm_device
*cdev
= to_mips_cdmm_device(dev
);
63 struct mips_cdmm_driver
*cdrv
= to_mips_cdmm_driver(drv
);
65 return mips_cdmm_lookup(cdrv
->id_table
, cdev
) != NULL
;
68 static int mips_cdmm_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
70 struct mips_cdmm_device
*cdev
= to_mips_cdmm_device(dev
);
73 retval
= add_uevent_var(env
, "CDMM_CPU=%u", cdev
->cpu
);
77 retval
= add_uevent_var(env
, "CDMM_TYPE=0x%02x", cdev
->type
);
81 retval
= add_uevent_var(env
, "CDMM_REV=%u", cdev
->rev
);
85 retval
= add_uevent_var(env
, "MODALIAS=mipscdmm:t%02X", cdev
->type
);
89 /* Device attributes */
91 #define CDMM_ATTR(name, fmt, arg...) \
92 static ssize_t name##_show(struct device *_dev, \
93 struct device_attribute *attr, char *buf) \
95 struct mips_cdmm_device *dev = to_mips_cdmm_device(_dev); \
96 return sprintf(buf, fmt, arg); \
98 static DEVICE_ATTR_RO(name);
100 CDMM_ATTR(cpu
, "%u\n", dev
->cpu
);
101 CDMM_ATTR(type
, "0x%02x\n", dev
->type
);
102 CDMM_ATTR(revision
, "%u\n", dev
->rev
);
103 CDMM_ATTR(modalias
, "mipscdmm:t%02X\n", dev
->type
);
104 CDMM_ATTR(resource
, "\t%016llx\t%016llx\t%016lx\n",
105 (unsigned long long)dev
->res
.start
,
106 (unsigned long long)dev
->res
.end
,
109 static struct attribute
*mips_cdmm_dev_attrs
[] = {
112 &dev_attr_revision
.attr
,
113 &dev_attr_modalias
.attr
,
114 &dev_attr_resource
.attr
,
117 ATTRIBUTE_GROUPS(mips_cdmm_dev
);
119 struct bus_type mips_cdmm_bustype
= {
121 .dev_groups
= mips_cdmm_dev_groups
,
122 .match
= mips_cdmm_match
,
123 .uevent
= mips_cdmm_uevent
,
125 EXPORT_SYMBOL_GPL(mips_cdmm_bustype
);
128 * Standard driver callback helpers.
130 * All the CDMM driver callbacks need to be executed on the appropriate CPU from
131 * workqueues. For the standard driver callbacks we need a work function
132 * (mips_cdmm_{void,int}_work()) to do the actual call from the right CPU, and a
133 * wrapper function (generated with BUILD_PERCPU_HELPER) to arrange for the work
134 * function to be called on that CPU.
138 * struct mips_cdmm_work_dev - Data for per-device call work.
139 * @fn: CDMM driver callback function to call for the device.
140 * @dev: CDMM device to pass to @fn.
142 struct mips_cdmm_work_dev
{
144 struct mips_cdmm_device
*dev
;
148 * mips_cdmm_void_work() - Call a void returning CDMM driver callback.
149 * @data: struct mips_cdmm_work_dev pointer.
151 * A work_on_cpu() callback function to call an arbitrary CDMM driver callback
152 * function which doesn't return a value.
154 static long mips_cdmm_void_work(void *data
)
156 struct mips_cdmm_work_dev
*work
= data
;
157 void (*fn
)(struct mips_cdmm_device
*) = work
->fn
;
164 * mips_cdmm_int_work() - Call an int returning CDMM driver callback.
165 * @data: struct mips_cdmm_work_dev pointer.
167 * A work_on_cpu() callback function to call an arbitrary CDMM driver callback
168 * function which returns an int.
170 static long mips_cdmm_int_work(void *data
)
172 struct mips_cdmm_work_dev
*work
= data
;
173 int (*fn
)(struct mips_cdmm_device
*) = work
->fn
;
175 return fn(work
->dev
);
178 #define _BUILD_RET_void
179 #define _BUILD_RET_int return
182 * BUILD_PERCPU_HELPER() - Helper to call a CDMM driver callback on right CPU.
183 * @_ret: Return type (void or int).
184 * @_name: Name of CDMM driver callback function.
186 * Generates a specific device callback function to call a CDMM driver callback
187 * function on the appropriate CPU for the device, and if applicable return the
190 #define BUILD_PERCPU_HELPER(_ret, _name) \
191 static _ret mips_cdmm_##_name(struct device *dev) \
193 struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev); \
194 struct mips_cdmm_driver *cdrv = to_mips_cdmm_driver(dev->driver); \
195 struct mips_cdmm_work_dev work = { \
200 _BUILD_RET_##_ret work_on_cpu(cdev->cpu, \
201 mips_cdmm_##_ret##_work, &work); \
204 /* Driver callback functions */
205 BUILD_PERCPU_HELPER(int, probe
) /* int mips_cdmm_probe(struct device) */
206 BUILD_PERCPU_HELPER(int, remove
) /* int mips_cdmm_remove(struct device) */
207 BUILD_PERCPU_HELPER(void, shutdown
) /* void mips_cdmm_shutdown(struct device) */
210 /* Driver registration */
213 * mips_cdmm_driver_register() - Register a CDMM driver.
214 * @drv: CDMM driver information.
216 * Register a CDMM driver with the CDMM subsystem. The driver will be informed
217 * of matching devices which are discovered.
219 * Returns: 0 on success.
221 int mips_cdmm_driver_register(struct mips_cdmm_driver
*drv
)
223 drv
->drv
.bus
= &mips_cdmm_bustype
;
226 drv
->drv
.probe
= mips_cdmm_probe
;
228 drv
->drv
.remove
= mips_cdmm_remove
;
230 drv
->drv
.shutdown
= mips_cdmm_shutdown
;
232 return driver_register(&drv
->drv
);
234 EXPORT_SYMBOL_GPL(mips_cdmm_driver_register
);
237 * mips_cdmm_driver_unregister() - Unregister a CDMM driver.
238 * @drv: CDMM driver information.
240 * Unregister a CDMM driver from the CDMM subsystem.
242 void mips_cdmm_driver_unregister(struct mips_cdmm_driver
*drv
)
244 driver_unregister(&drv
->drv
);
246 EXPORT_SYMBOL_GPL(mips_cdmm_driver_unregister
);
249 /* CDMM initialisation and bus discovery */
252 * struct mips_cdmm_bus - Info about CDMM bus.
253 * @phys: Physical address at which it is mapped.
254 * @regs: Virtual address where registers can be accessed.
255 * @drbs: Total number of DRBs.
256 * @drbs_reserved: Number of DRBs reserved.
257 * @discovered: Whether the devices on the bus have been discovered yet.
258 * @offline: Whether the CDMM bus is going offline (or very early
259 * coming back online), in which case it should be
260 * reconfigured each time.
262 struct mips_cdmm_bus
{
266 unsigned int drbs_reserved
;
271 static struct mips_cdmm_bus mips_cdmm_boot_bus
;
272 static DEFINE_PER_CPU(struct mips_cdmm_bus
*, mips_cdmm_buses
);
273 static atomic_t mips_cdmm_next_id
= ATOMIC_INIT(-1);
276 * mips_cdmm_get_bus() - Get the per-CPU CDMM bus information.
278 * Get information about the per-CPU CDMM bus, if the bus is present.
280 * The caller must prevent migration to another CPU, either by disabling
281 * pre-emption or by running from a pinned kernel thread.
283 * Returns: Pointer to CDMM bus information for the current CPU.
284 * May return ERR_PTR(-errno) in case of error, so check with
287 static struct mips_cdmm_bus
*mips_cdmm_get_bus(void)
289 struct mips_cdmm_bus
*bus
, **bus_p
;
294 return ERR_PTR(-ENODEV
);
296 cpu
= smp_processor_id();
297 /* Avoid early use of per-cpu primitives before initialised */
299 return &mips_cdmm_boot_bus
;
301 /* Get bus pointer */
302 bus_p
= per_cpu_ptr(&mips_cdmm_buses
, cpu
);
303 local_irq_save(flags
);
305 /* Attempt allocation if NULL */
306 if (unlikely(!bus
)) {
307 bus
= kzalloc(sizeof(*bus
), GFP_ATOMIC
);
309 bus
= ERR_PTR(-ENOMEM
);
313 local_irq_restore(flags
);
318 * mips_cdmm_cur_base() - Find current physical base address of CDMM region.
320 * Returns: Physical base address of CDMM region according to cdmmbase CP0
321 * register, or 0 if the CDMM region is disabled.
323 static phys_addr_t
mips_cdmm_cur_base(void)
325 unsigned long cdmmbase
= read_c0_cdmmbase();
327 if (!(cdmmbase
& MIPS_CDMMBASE_EN
))
330 return (cdmmbase
>> MIPS_CDMMBASE_ADDR_SHIFT
)
331 << MIPS_CDMMBASE_ADDR_START
;
335 * mips_cdmm_phys_base() - Choose a physical base address for CDMM region.
337 * Picking a suitable physical address at which to map the CDMM region is
338 * platform specific, so this weak function can be overridden by platform
339 * code to pick a suitable value if none is configured by the bootloader.
341 phys_addr_t __weak
mips_cdmm_phys_base(void)
347 * mips_cdmm_setup() - Ensure the CDMM bus is initialised and usable.
348 * @bus: Pointer to bus information for current CPU.
349 * IS_ERR(bus) is checked, so no need for caller to check.
351 * The caller must prevent migration to another CPU, either by disabling
352 * pre-emption or by running from a pinned kernel thread.
354 * Returns 0 on success, -errno on failure.
356 static int mips_cdmm_setup(struct mips_cdmm_bus
*bus
)
358 unsigned long cdmmbase
, flags
;
364 local_irq_save(flags
);
365 /* Don't set up bus a second time unless marked offline */
367 /* If CDMM region is still set up, nothing to do */
368 if (bus
->phys
== mips_cdmm_cur_base())
371 * The CDMM region isn't set up as expected, so it needs
372 * reconfiguring, but then we can stop checking it.
374 bus
->offline
= false;
375 } else if (bus
->phys
> 1) {
379 /* If the CDMM region is already configured, inherit that setup */
381 bus
->phys
= mips_cdmm_cur_base();
382 /* Otherwise, ask platform code for suggestions */
384 bus
->phys
= mips_cdmm_phys_base();
385 /* Otherwise, copy what other CPUs have done */
387 bus
->phys
= mips_cdmm_default_base
;
388 /* Otherwise, complain once */
392 * If you hit this, either your bootloader needs to set up the
393 * CDMM on the boot CPU, or else you need to implement
394 * mips_cdmm_phys_base() for your platform (see asm/cdmm.h).
396 pr_err("cdmm%u: Failed to choose a physical base\n",
399 /* Already complained? */
400 if (bus
->phys
== 1) {
404 /* Record our success for other CPUs to copy */
405 mips_cdmm_default_base
= bus
->phys
;
407 pr_debug("cdmm%u: Enabling CDMM region at %pa\n",
408 smp_processor_id(), &bus
->phys
);
411 cdmmbase
= read_c0_cdmmbase();
412 cdmmbase
&= (1ul << MIPS_CDMMBASE_ADDR_SHIFT
) - 1;
413 cdmmbase
|= (bus
->phys
>> MIPS_CDMMBASE_ADDR_START
)
414 << MIPS_CDMMBASE_ADDR_SHIFT
;
415 cdmmbase
|= MIPS_CDMMBASE_EN
;
416 write_c0_cdmmbase(cdmmbase
);
419 bus
->regs
= (void __iomem
*)CKSEG1ADDR(bus
->phys
);
420 bus
->drbs
= 1 + ((cdmmbase
& MIPS_CDMMBASE_SIZE
) >>
421 MIPS_CDMMBASE_SIZE_SHIFT
);
422 bus
->drbs_reserved
= !!(cdmmbase
& MIPS_CDMMBASE_CI
);
425 local_irq_restore(flags
);
430 * mips_cdmm_early_probe() - Minimally probe for a specific device on CDMM.
431 * @dev_type: CDMM type code to look for.
433 * Minimally configure the in-CPU Common Device Memory Map (CDMM) and look for a
434 * specific device. This can be used to find a device very early in boot for
435 * example to configure an early FDC console device.
437 * The caller must prevent migration to another CPU, either by disabling
438 * pre-emption or by running from a pinned kernel thread.
440 * Returns: MMIO pointer to device memory. The caller can read the ACSR
441 * register to find more information about the device (such as the
442 * version number or the number of blocks).
443 * May return IOMEM_ERR_PTR(-errno) in case of error, so check with
446 void __iomem
*mips_cdmm_early_probe(unsigned int dev_type
)
448 struct mips_cdmm_bus
*bus
;
451 unsigned int drb
, type
, size
;
454 if (WARN_ON(!dev_type
))
455 return IOMEM_ERR_PTR(-ENODEV
);
457 bus
= mips_cdmm_get_bus();
458 err
= mips_cdmm_setup(bus
);
460 return IOMEM_ERR_PTR(err
);
462 /* Skip the first block if it's reserved for more registers */
463 drb
= bus
->drbs_reserved
;
466 /* Look for a specific device type */
467 for (; drb
< bus
->drbs
; drb
+= size
+ 1) {
468 acsr
= __raw_readl(cdmm
+ drb
* CDMM_DRB_SIZE
);
469 type
= (acsr
& CDMM_ACSR_DEVTYPE
) >> CDMM_ACSR_DEVTYPE_SHIFT
;
470 if (type
== dev_type
)
471 return cdmm
+ drb
* CDMM_DRB_SIZE
;
472 size
= (acsr
& CDMM_ACSR_DEVSIZE
) >> CDMM_ACSR_DEVSIZE_SHIFT
;
475 return IOMEM_ERR_PTR(-ENODEV
);
477 EXPORT_SYMBOL_GPL(mips_cdmm_early_probe
);
480 * mips_cdmm_release() - Release a removed CDMM device.
481 * @dev: Device object
483 * Clean up the struct mips_cdmm_device for an unused CDMM device. This is
484 * called automatically by the driver core when a device is removed.
486 static void mips_cdmm_release(struct device
*dev
)
488 struct mips_cdmm_device
*cdev
= to_mips_cdmm_device(dev
);
494 * mips_cdmm_bus_discover() - Discover the devices on the CDMM bus.
495 * @bus: CDMM bus information, must already be set up.
497 static void mips_cdmm_bus_discover(struct mips_cdmm_bus
*bus
)
501 unsigned int drb
, type
, size
, rev
;
502 struct mips_cdmm_device
*dev
;
503 unsigned int cpu
= smp_processor_id();
507 /* Skip the first block if it's reserved for more registers */
508 drb
= bus
->drbs_reserved
;
511 /* Discover devices */
512 bus
->discovered
= true;
513 pr_info("cdmm%u discovery (%u blocks)\n", cpu
, bus
->drbs
);
514 for (; drb
< bus
->drbs
; drb
+= size
+ 1) {
515 acsr
= __raw_readl(cdmm
+ drb
* CDMM_DRB_SIZE
);
516 type
= (acsr
& CDMM_ACSR_DEVTYPE
) >> CDMM_ACSR_DEVTYPE_SHIFT
;
517 size
= (acsr
& CDMM_ACSR_DEVSIZE
) >> CDMM_ACSR_DEVSIZE_SHIFT
;
518 rev
= (acsr
& CDMM_ACSR_DEVREV
) >> CDMM_ACSR_DEVREV_SHIFT
;
523 pr_info("cdmm%u-%u: @%u (%#x..%#x), type 0x%02x, rev %u\n",
524 cpu
, id
, drb
, drb
* CDMM_DRB_SIZE
,
525 (drb
+ size
+ 1) * CDMM_DRB_SIZE
- 1,
528 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
533 dev
->res
.start
= bus
->phys
+ drb
* CDMM_DRB_SIZE
;
534 dev
->res
.end
= bus
->phys
+
535 (drb
+ size
+ 1) * CDMM_DRB_SIZE
- 1;
536 dev
->res
.flags
= IORESOURCE_MEM
;
539 dev
->dev
.parent
= get_cpu_device(cpu
);
540 dev
->dev
.bus
= &mips_cdmm_bustype
;
541 dev
->dev
.id
= atomic_inc_return(&mips_cdmm_next_id
);
542 dev
->dev
.release
= mips_cdmm_release
;
544 dev_set_name(&dev
->dev
, "cdmm%u-%u", cpu
, id
);
546 ret
= device_register(&dev
->dev
);
548 put_device(&dev
->dev
);
556 * CPU hotplug and initialisation
558 * All the CDMM driver callbacks need to be executed on the appropriate CPU from
559 * workqueues. For the CPU callbacks, they need to be called for all devices on
560 * that CPU, so the work function calls bus_for_each_dev, using a helper
561 * (generated with BUILD_PERDEV_HELPER) to call the driver callback if the
562 * device's CPU matches.
566 * BUILD_PERDEV_HELPER() - Helper to call a CDMM driver callback if CPU matches.
567 * @_name: Name of CDMM driver callback function.
569 * Generates a bus_for_each_dev callback function to call a specific CDMM driver
570 * callback function for the device if the device's CPU matches that pointed to
571 * by the data argument.
573 * This is used for informing drivers for all devices on a given CPU of some
574 * event (such as the CPU going online/offline).
576 * It is expected to already be called from the appropriate CPU.
578 #define BUILD_PERDEV_HELPER(_name) \
579 static int mips_cdmm_##_name##_helper(struct device *dev, void *data) \
581 struct mips_cdmm_device *cdev = to_mips_cdmm_device(dev); \
582 struct mips_cdmm_driver *cdrv; \
583 unsigned int cpu = *(unsigned int *)data; \
585 if (cdev->cpu != cpu || !dev->driver) \
588 cdrv = to_mips_cdmm_driver(dev->driver); \
591 return cdrv->_name(cdev); \
594 /* bus_for_each_dev callback helper functions */
595 BUILD_PERDEV_HELPER(cpu_down
) /* int mips_cdmm_cpu_down_helper(...) */
596 BUILD_PERDEV_HELPER(cpu_up
) /* int mips_cdmm_cpu_up_helper(...) */
599 * mips_cdmm_bus_down() - Tear down the CDMM bus.
600 * @data: Pointer to unsigned int CPU number.
602 * This work_on_cpu callback function is executed on a given CPU to call the
603 * CDMM driver cpu_down callback for all devices on that CPU.
605 static long mips_cdmm_bus_down(void *data
)
607 struct mips_cdmm_bus
*bus
;
610 /* Inform all the devices on the bus */
611 ret
= bus_for_each_dev(&mips_cdmm_bustype
, NULL
, data
,
612 mips_cdmm_cpu_down_helper
);
615 * While bus is offline, each use of it should reconfigure it just in
616 * case it is first use when coming back online again.
618 bus
= mips_cdmm_get_bus();
626 * mips_cdmm_bus_up() - Bring up the CDMM bus.
627 * @data: Pointer to unsigned int CPU number.
629 * This work_on_cpu callback function is executed on a given CPU to discover
630 * CDMM devices on that CPU, or to call the CDMM driver cpu_up callback for all
631 * devices already discovered on that CPU.
633 * It is used during initialisation and when CPUs are brought online.
635 static long mips_cdmm_bus_up(void *data
)
637 struct mips_cdmm_bus
*bus
;
640 bus
= mips_cdmm_get_bus();
641 ret
= mips_cdmm_setup(bus
);
645 /* Bus now set up, so we can drop the offline flag if still set */
646 bus
->offline
= false;
648 if (!bus
->discovered
)
649 mips_cdmm_bus_discover(bus
);
651 /* Inform all the devices on the bus */
652 ret
= bus_for_each_dev(&mips_cdmm_bustype
, NULL
, data
,
653 mips_cdmm_cpu_up_helper
);
659 * mips_cdmm_cpu_notify() - Take action when a CPU is going online or offline.
660 * @nb: CPU notifier block .
661 * @action: Event that has taken place (CPU_*).
664 * This notifier is used to keep the CDMM buses updated as CPUs are offlined and
665 * onlined. When CPUs go offline or come back online, so does their CDMM bus, so
666 * devices must be informed. Also when CPUs come online for the first time the
667 * devices on the CDMM bus need discovering.
669 * Returns: NOTIFY_OK if event was used.
670 * NOTIFY_DONE if we didn't care.
672 static int mips_cdmm_cpu_notify(struct notifier_block
*nb
,
673 unsigned long action
, void *data
)
675 unsigned int cpu
= (unsigned int)data
;
677 switch (action
& ~CPU_TASKS_FROZEN
) {
679 case CPU_DOWN_FAILED
:
680 work_on_cpu(cpu
, mips_cdmm_bus_up
, &cpu
);
682 case CPU_DOWN_PREPARE
:
683 work_on_cpu(cpu
, mips_cdmm_bus_down
, &cpu
);
692 static struct notifier_block mips_cdmm_cpu_nb
= {
693 .notifier_call
= mips_cdmm_cpu_notify
,
697 * mips_cdmm_init() - Initialise CDMM bus.
699 * Initialise CDMM bus, discover CDMM devices for online CPUs, and arrange for
700 * hotplug notifications so the CDMM drivers can be kept up to date.
702 static int __init
mips_cdmm_init(void)
707 /* Register the bus */
708 ret
= bus_register(&mips_cdmm_bustype
);
712 /* We want to be notified about new CPUs */
713 ret
= register_cpu_notifier(&mips_cdmm_cpu_nb
);
715 pr_warn("cdmm: Failed to register CPU notifier\n");
719 /* Discover devices on CDMM of online CPUs */
720 for_each_online_cpu(cpu
)
721 work_on_cpu(cpu
, mips_cdmm_bus_up
, &cpu
);
725 bus_unregister(&mips_cdmm_bustype
);
728 subsys_initcall(mips_cdmm_init
);