1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/mach-orion5x/ts78xx-setup.c
5 * Maintainer: Alexander Clouter <alex@digriz.org.uk>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/sysfs.h>
13 #include <linux/platform_device.h>
14 #include <linux/mv643xx_eth.h>
15 #include <linux/ata_platform.h>
16 #include <linux/mtd/platnand.h>
17 #include <linux/timeriomem-rng.h>
18 #include <asm/mach-types.h>
19 #include <asm/mach/arch.h>
20 #include <asm/mach/map.h>
24 #include "ts78xx-fpga.h"
26 /*****************************************************************************
28 ****************************************************************************/
31 * FPGA - lives where the PCI bus would be at ORION5X_PCI_MEM_PHYS_BASE
33 #define TS78XX_FPGA_REGS_PHYS_BASE 0xe8000000
34 #define TS78XX_FPGA_REGS_VIRT_BASE IOMEM(0xff900000)
35 #define TS78XX_FPGA_REGS_SIZE SZ_1M
37 static struct ts78xx_fpga_data ts78xx_fpga = {
40 /* .supports = ... - populated by ts78xx_fpga_supports() */
43 /*****************************************************************************
45 ****************************************************************************/
46 static struct map_desc ts78xx_io_desc[] __initdata = {
48 .virtual = (unsigned long)TS78XX_FPGA_REGS_VIRT_BASE,
49 .pfn = __phys_to_pfn(TS78XX_FPGA_REGS_PHYS_BASE),
50 .length = TS78XX_FPGA_REGS_SIZE,
55 static void __init ts78xx_map_io(void)
58 iotable_init(ts78xx_io_desc, ARRAY_SIZE(ts78xx_io_desc));
61 /*****************************************************************************
63 ****************************************************************************/
64 static struct mv643xx_eth_platform_data ts78xx_eth_data = {
65 .phy_addr = MV643XX_ETH_PHY_ADDR(0),
68 /*****************************************************************************
70 ****************************************************************************/
71 static struct mv_sata_platform_data ts78xx_sata_data = {
75 /*****************************************************************************
76 * RTC M48T86 - nicked^Wborrowed from arch/arm/mach-ep93xx/ts72xx.c
77 ****************************************************************************/
78 #define TS_RTC_CTRL (TS78XX_FPGA_REGS_PHYS_BASE + 0x808)
79 #define TS_RTC_DATA (TS78XX_FPGA_REGS_PHYS_BASE + 0x80c)
81 static struct resource ts78xx_ts_rtc_resources[] = {
82 DEFINE_RES_MEM(TS_RTC_CTRL, 0x01),
83 DEFINE_RES_MEM(TS_RTC_DATA, 0x01),
86 static struct platform_device ts78xx_ts_rtc_device = {
89 .resource = ts78xx_ts_rtc_resources,
90 .num_resources = ARRAY_SIZE(ts78xx_ts_rtc_resources),
93 static int ts78xx_ts_rtc_load(void)
97 if (ts78xx_fpga.supports.ts_rtc.init == 0) {
98 rc = platform_device_register(&ts78xx_ts_rtc_device);
100 ts78xx_fpga.supports.ts_rtc.init = 1;
102 rc = platform_device_add(&ts78xx_ts_rtc_device);
106 pr_info("RTC could not be registered: %d\n", rc);
111 static void ts78xx_ts_rtc_unload(void)
113 platform_device_del(&ts78xx_ts_rtc_device);
116 /*****************************************************************************
118 ****************************************************************************/
119 #define TS_NAND_CTRL (TS78XX_FPGA_REGS_VIRT_BASE + 0x800) /* VIRT */
120 #define TS_NAND_DATA (TS78XX_FPGA_REGS_PHYS_BASE + 0x804) /* PHYS */
123 * hardware specific access to control-lines
126 * NAND_NCE: bit 0 -> bit 2
127 * NAND_CLE: bit 1 -> bit 1
128 * NAND_ALE: bit 2 -> bit 0
130 static void ts78xx_ts_nand_cmd_ctrl(struct nand_chip *this, int cmd,
133 if (ctrl & NAND_CTRL_CHANGE) {
136 bits = (ctrl & NAND_NCE) << 2;
137 bits |= ctrl & NAND_CLE;
138 bits |= (ctrl & NAND_ALE) >> 2;
140 writeb((readb(TS_NAND_CTRL) & ~0x7) | bits, TS_NAND_CTRL);
143 if (cmd != NAND_CMD_NONE)
144 writeb(cmd, this->legacy.IO_ADDR_W);
147 static int ts78xx_ts_nand_dev_ready(struct nand_chip *chip)
149 return readb(TS_NAND_CTRL) & 0x20;
152 static void ts78xx_ts_nand_write_buf(struct nand_chip *chip,
153 const uint8_t *buf, int len)
155 void __iomem *io_base = chip->legacy.IO_ADDR_W;
156 unsigned long off = ((unsigned long)buf & 3);
160 sz = min_t(int, 4 - off, len);
161 writesb(io_base, buf, sz);
168 u32 *buf32 = (u32 *)buf;
169 writesl(io_base, buf32, sz);
175 writesb(io_base, buf, len);
178 static void ts78xx_ts_nand_read_buf(struct nand_chip *chip,
179 uint8_t *buf, int len)
181 void __iomem *io_base = chip->legacy.IO_ADDR_R;
182 unsigned long off = ((unsigned long)buf & 3);
186 sz = min_t(int, 4 - off, len);
187 readsb(io_base, buf, sz);
194 u32 *buf32 = (u32 *)buf;
195 readsl(io_base, buf32, sz);
201 readsb(io_base, buf, len);
204 static struct mtd_partition ts78xx_ts_nand_parts[] = {
209 .mask_flags = MTD_WRITEABLE,
212 .offset = MTDPART_OFS_APPEND,
216 .offset = MTDPART_OFS_APPEND,
220 .offset = MTDPART_OFS_APPEND,
221 .size = MTDPART_SIZ_FULL,
225 static struct platform_nand_data ts78xx_ts_nand_data = {
228 .partitions = ts78xx_ts_nand_parts,
229 .nr_partitions = ARRAY_SIZE(ts78xx_ts_nand_parts),
231 .bbt_options = NAND_BBT_USE_FLASH,
235 * The HW ECC offloading functions, used to give about a 9%
236 * performance increase for 'dd if=/dev/mtdblockX' and 5% for
237 * nanddump. This all however was changed by git commit
238 * e6cf5df1838c28bb060ac45b5585e48e71bbc740 so now there is
239 * no performance advantage to be had so we no longer bother
241 .cmd_ctrl = ts78xx_ts_nand_cmd_ctrl,
242 .dev_ready = ts78xx_ts_nand_dev_ready,
243 .write_buf = ts78xx_ts_nand_write_buf,
244 .read_buf = ts78xx_ts_nand_read_buf,
248 static struct resource ts78xx_ts_nand_resources
249 = DEFINE_RES_MEM(TS_NAND_DATA, 4);
251 static struct platform_device ts78xx_ts_nand_device = {
255 .platform_data = &ts78xx_ts_nand_data,
257 .resource = &ts78xx_ts_nand_resources,
261 static int ts78xx_ts_nand_load(void)
265 if (ts78xx_fpga.supports.ts_nand.init == 0) {
266 rc = platform_device_register(&ts78xx_ts_nand_device);
268 ts78xx_fpga.supports.ts_nand.init = 1;
270 rc = platform_device_add(&ts78xx_ts_nand_device);
273 pr_info("NAND could not be registered: %d\n", rc);
277 static void ts78xx_ts_nand_unload(void)
279 platform_device_del(&ts78xx_ts_nand_device);
282 /*****************************************************************************
284 ****************************************************************************/
285 #define TS_RNG_DATA (TS78XX_FPGA_REGS_PHYS_BASE | 0x044)
287 static struct resource ts78xx_ts_rng_resource
288 = DEFINE_RES_MEM(TS_RNG_DATA, 4);
290 static struct timeriomem_rng_data ts78xx_ts_rng_data = {
291 .period = 1000000, /* one second */
294 static struct platform_device ts78xx_ts_rng_device = {
295 .name = "timeriomem_rng",
298 .platform_data = &ts78xx_ts_rng_data,
300 .resource = &ts78xx_ts_rng_resource,
304 static int ts78xx_ts_rng_load(void)
308 if (ts78xx_fpga.supports.ts_rng.init == 0) {
309 rc = platform_device_register(&ts78xx_ts_rng_device);
311 ts78xx_fpga.supports.ts_rng.init = 1;
313 rc = platform_device_add(&ts78xx_ts_rng_device);
316 pr_info("RNG could not be registered: %d\n", rc);
320 static void ts78xx_ts_rng_unload(void)
322 platform_device_del(&ts78xx_ts_rng_device);
325 /*****************************************************************************
326 * FPGA 'hotplug' support code
327 ****************************************************************************/
328 static void ts78xx_fpga_devices_zero_init(void)
330 ts78xx_fpga.supports.ts_rtc.init = 0;
331 ts78xx_fpga.supports.ts_nand.init = 0;
332 ts78xx_fpga.supports.ts_rng.init = 0;
335 static void ts78xx_fpga_supports(void)
337 /* TODO: put this 'table' into ts78xx-fpga.h */
338 switch (ts78xx_fpga.id) {
348 ts78xx_fpga.supports.ts_rtc.present = 1;
349 ts78xx_fpga.supports.ts_nand.present = 1;
350 ts78xx_fpga.supports.ts_rng.present = 1;
353 /* enable devices if magic matches */
354 switch ((ts78xx_fpga.id >> 8) & 0xffffff) {
355 case TS7800_FPGA_MAGIC:
356 pr_warn("unrecognised FPGA revision 0x%.2x\n",
357 ts78xx_fpga.id & 0xff);
358 ts78xx_fpga.supports.ts_rtc.present = 1;
359 ts78xx_fpga.supports.ts_nand.present = 1;
360 ts78xx_fpga.supports.ts_rng.present = 1;
363 ts78xx_fpga.supports.ts_rtc.present = 0;
364 ts78xx_fpga.supports.ts_nand.present = 0;
365 ts78xx_fpga.supports.ts_rng.present = 0;
370 static int ts78xx_fpga_load_devices(void)
374 if (ts78xx_fpga.supports.ts_rtc.present == 1) {
375 tmp = ts78xx_ts_rtc_load();
377 ts78xx_fpga.supports.ts_rtc.present = 0;
380 if (ts78xx_fpga.supports.ts_nand.present == 1) {
381 tmp = ts78xx_ts_nand_load();
383 ts78xx_fpga.supports.ts_nand.present = 0;
386 if (ts78xx_fpga.supports.ts_rng.present == 1) {
387 tmp = ts78xx_ts_rng_load();
389 ts78xx_fpga.supports.ts_rng.present = 0;
396 static int ts78xx_fpga_unload_devices(void)
399 if (ts78xx_fpga.supports.ts_rtc.present == 1)
400 ts78xx_ts_rtc_unload();
401 if (ts78xx_fpga.supports.ts_nand.present == 1)
402 ts78xx_ts_nand_unload();
403 if (ts78xx_fpga.supports.ts_rng.present == 1)
404 ts78xx_ts_rng_unload();
409 static int ts78xx_fpga_load(void)
411 ts78xx_fpga.id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
413 pr_info("FPGA magic=0x%.6x, rev=0x%.2x\n",
414 (ts78xx_fpga.id >> 8) & 0xffffff,
415 ts78xx_fpga.id & 0xff);
417 ts78xx_fpga_supports();
419 if (ts78xx_fpga_load_devices()) {
420 ts78xx_fpga.state = -1;
427 static int ts78xx_fpga_unload(void)
429 unsigned int fpga_id;
431 fpga_id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
434 * There does not seem to be a feasible way to block access to the GPIO
435 * pins from userspace (/dev/mem). This if clause should hopefully warn
436 * those foolish enough not to follow 'policy' :)
438 * UrJTAG SVN since r1381 can be used to reprogram the FPGA
440 if (ts78xx_fpga.id != fpga_id) {
441 pr_err("FPGA magic/rev mismatch\n"
442 "TS-78xx FPGA: was 0x%.6x/%.2x but now 0x%.6x/%.2x\n",
443 (ts78xx_fpga.id >> 8) & 0xffffff, ts78xx_fpga.id & 0xff,
444 (fpga_id >> 8) & 0xffffff, fpga_id & 0xff);
445 ts78xx_fpga.state = -1;
449 if (ts78xx_fpga_unload_devices()) {
450 ts78xx_fpga.state = -1;
457 static ssize_t ts78xx_fpga_show(struct kobject *kobj,
458 struct kobj_attribute *attr, char *buf)
460 if (ts78xx_fpga.state < 0)
461 return sprintf(buf, "borked\n");
463 return sprintf(buf, "%s\n", (ts78xx_fpga.state) ? "online" : "offline");
466 static ssize_t ts78xx_fpga_store(struct kobject *kobj,
467 struct kobj_attribute *attr, const char *buf, size_t n)
471 if (ts78xx_fpga.state < 0) {
472 pr_err("FPGA borked, you must powercycle ASAP\n");
476 if (strncmp(buf, "online", sizeof("online") - 1) == 0)
478 else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
483 if (ts78xx_fpga.state == value)
486 ret = (ts78xx_fpga.state == 0)
488 : ts78xx_fpga_unload();
491 ts78xx_fpga.state = value;
496 static struct kobj_attribute ts78xx_fpga_attr =
497 __ATTR(ts78xx_fpga, 0644, ts78xx_fpga_show, ts78xx_fpga_store);
499 /*****************************************************************************
501 ****************************************************************************/
502 static unsigned int ts78xx_mpp_modes[] __initdata = {
504 MPP1_GPIO, /* JTAG Clock */
505 MPP2_GPIO, /* JTAG Data In */
506 MPP3_GPIO, /* Lat ECP2 256 FPGA - PB2B */
507 MPP4_GPIO, /* JTAG Data Out */
508 MPP5_GPIO, /* JTAG TMS */
509 MPP6_GPIO, /* Lat ECP2 256 FPGA - PB31A_CLK4+ */
510 MPP7_GPIO, /* Lat ECP2 256 FPGA - PB22B */
524 * MPP[20] PCI Clock Out 1
525 * MPP[21] PCI Clock Out 0
534 static void __init ts78xx_init(void)
539 * Setup basic Orion functions. Need to be called early.
543 orion5x_mpp_conf(ts78xx_mpp_modes);
546 * Configure peripherals.
548 orion5x_ehci0_init();
549 orion5x_ehci1_init();
550 orion5x_eth_init(&ts78xx_eth_data);
551 orion5x_sata_init(&ts78xx_sata_data);
552 orion5x_uart0_init();
553 orion5x_uart1_init();
557 ts78xx_fpga_devices_zero_init();
558 ret = ts78xx_fpga_load();
559 ret = sysfs_create_file(firmware_kobj, &ts78xx_fpga_attr.attr);
561 pr_err("sysfs_create_file failed: %d\n", ret);
564 MACHINE_START(TS78XX, "Technologic Systems TS-78xx SBC")
565 /* Maintainer: Alexander Clouter <alex@digriz.org.uk> */
566 .atag_offset = 0x100,
567 .nr_irqs = ORION5X_NR_IRQS,
568 .init_machine = ts78xx_init,
569 .map_io = ts78xx_map_io,
570 .init_early = orion5x_init_early,
571 .init_irq = orion5x_init_irq,
572 .init_time = orion5x_timer_init,
573 .restart = orion5x_restart,