ts78xx-setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * arch/arm/mach-orion5x/ts78xx-setup.c
  3. *
  4. * Maintainer: Alexander Clouter <alex@digriz.org.uk>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/sysfs.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mv643xx_eth.h>
  16. #include <linux/ata_platform.h>
  17. #include <linux/mtd/platnand.h>
  18. #include <linux/timeriomem-rng.h>
  19. #include <asm/mach-types.h>
  20. #include <asm/mach/arch.h>
  21. #include <asm/mach/map.h>
  22. #include "common.h"
  23. #include "mpp.h"
  24. #include "orion5x.h"
  25. #include "ts78xx-fpga.h"
  26. /*****************************************************************************
  27. * TS-78xx Info
  28. ****************************************************************************/
  29. /*
  30. * FPGA - lives where the PCI bus would be at ORION5X_PCI_MEM_PHYS_BASE
  31. */
  32. #define TS78XX_FPGA_REGS_PHYS_BASE 0xe8000000
  33. #define TS78XX_FPGA_REGS_VIRT_BASE IOMEM(0xff900000)
  34. #define TS78XX_FPGA_REGS_SIZE SZ_1M
  35. static struct ts78xx_fpga_data ts78xx_fpga = {
  36. .id = 0,
  37. .state = 1,
  38. /* .supports = ... - populated by ts78xx_fpga_supports() */
  39. };
  40. /*****************************************************************************
  41. * I/O Address Mapping
  42. ****************************************************************************/
  43. static struct map_desc ts78xx_io_desc[] __initdata = {
  44. {
  45. .virtual = (unsigned long)TS78XX_FPGA_REGS_VIRT_BASE,
  46. .pfn = __phys_to_pfn(TS78XX_FPGA_REGS_PHYS_BASE),
  47. .length = TS78XX_FPGA_REGS_SIZE,
  48. .type = MT_DEVICE,
  49. },
  50. };
  51. static void __init ts78xx_map_io(void)
  52. {
  53. orion5x_map_io();
  54. iotable_init(ts78xx_io_desc, ARRAY_SIZE(ts78xx_io_desc));
  55. }
  56. /*****************************************************************************
  57. * Ethernet
  58. ****************************************************************************/
  59. static struct mv643xx_eth_platform_data ts78xx_eth_data = {
  60. .phy_addr = MV643XX_ETH_PHY_ADDR(0),
  61. };
  62. /*****************************************************************************
  63. * SATA
  64. ****************************************************************************/
  65. static struct mv_sata_platform_data ts78xx_sata_data = {
  66. .n_ports = 2,
  67. };
  68. /*****************************************************************************
  69. * RTC M48T86 - nicked^Wborrowed from arch/arm/mach-ep93xx/ts72xx.c
  70. ****************************************************************************/
  71. #define TS_RTC_CTRL (TS78XX_FPGA_REGS_PHYS_BASE + 0x808)
  72. #define TS_RTC_DATA (TS78XX_FPGA_REGS_PHYS_BASE + 0x80c)
  73. static struct resource ts78xx_ts_rtc_resources[] = {
  74. DEFINE_RES_MEM(TS_RTC_CTRL, 0x01),
  75. DEFINE_RES_MEM(TS_RTC_DATA, 0x01),
  76. };
  77. static struct platform_device ts78xx_ts_rtc_device = {
  78. .name = "rtc-m48t86",
  79. .id = -1,
  80. .resource = ts78xx_ts_rtc_resources,
  81. .num_resources = ARRAY_SIZE(ts78xx_ts_rtc_resources),
  82. };
  83. static int ts78xx_ts_rtc_load(void)
  84. {
  85. int rc;
  86. if (ts78xx_fpga.supports.ts_rtc.init == 0) {
  87. rc = platform_device_register(&ts78xx_ts_rtc_device);
  88. if (!rc)
  89. ts78xx_fpga.supports.ts_rtc.init = 1;
  90. } else {
  91. rc = platform_device_add(&ts78xx_ts_rtc_device);
  92. }
  93. if (rc)
  94. pr_info("RTC could not be registered: %d\n", rc);
  95. return rc;
  96. }
  97. static void ts78xx_ts_rtc_unload(void)
  98. {
  99. platform_device_del(&ts78xx_ts_rtc_device);
  100. }
  101. /*****************************************************************************
  102. * NAND Flash
  103. ****************************************************************************/
  104. #define TS_NAND_CTRL (TS78XX_FPGA_REGS_VIRT_BASE + 0x800) /* VIRT */
  105. #define TS_NAND_DATA (TS78XX_FPGA_REGS_PHYS_BASE + 0x804) /* PHYS */
  106. /*
  107. * hardware specific access to control-lines
  108. *
  109. * ctrl:
  110. * NAND_NCE: bit 0 -> bit 2
  111. * NAND_CLE: bit 1 -> bit 1
  112. * NAND_ALE: bit 2 -> bit 0
  113. */
  114. static void ts78xx_ts_nand_cmd_ctrl(struct nand_chip *this, int cmd,
  115. unsigned int ctrl)
  116. {
  117. if (ctrl & NAND_CTRL_CHANGE) {
  118. unsigned char bits;
  119. bits = (ctrl & NAND_NCE) << 2;
  120. bits |= ctrl & NAND_CLE;
  121. bits |= (ctrl & NAND_ALE) >> 2;
  122. writeb((readb(TS_NAND_CTRL) & ~0x7) | bits, TS_NAND_CTRL);
  123. }
  124. if (cmd != NAND_CMD_NONE)
  125. writeb(cmd, this->legacy.IO_ADDR_W);
  126. }
  127. static int ts78xx_ts_nand_dev_ready(struct nand_chip *chip)
  128. {
  129. return readb(TS_NAND_CTRL) & 0x20;
  130. }
  131. static void ts78xx_ts_nand_write_buf(struct nand_chip *chip,
  132. const uint8_t *buf, int len)
  133. {
  134. void __iomem *io_base = chip->legacy.IO_ADDR_W;
  135. unsigned long off = ((unsigned long)buf & 3);
  136. int sz;
  137. if (off) {
  138. sz = min_t(int, 4 - off, len);
  139. writesb(io_base, buf, sz);
  140. buf += sz;
  141. len -= sz;
  142. }
  143. sz = len >> 2;
  144. if (sz) {
  145. u32 *buf32 = (u32 *)buf;
  146. writesl(io_base, buf32, sz);
  147. buf += sz << 2;
  148. len -= sz << 2;
  149. }
  150. if (len)
  151. writesb(io_base, buf, len);
  152. }
  153. static void ts78xx_ts_nand_read_buf(struct nand_chip *chip,
  154. uint8_t *buf, int len)
  155. {
  156. void __iomem *io_base = chip->legacy.IO_ADDR_R;
  157. unsigned long off = ((unsigned long)buf & 3);
  158. int sz;
  159. if (off) {
  160. sz = min_t(int, 4 - off, len);
  161. readsb(io_base, buf, sz);
  162. buf += sz;
  163. len -= sz;
  164. }
  165. sz = len >> 2;
  166. if (sz) {
  167. u32 *buf32 = (u32 *)buf;
  168. readsl(io_base, buf32, sz);
  169. buf += sz << 2;
  170. len -= sz << 2;
  171. }
  172. if (len)
  173. readsb(io_base, buf, len);
  174. }
  175. static struct mtd_partition ts78xx_ts_nand_parts[] = {
  176. {
  177. .name = "mbr",
  178. .offset = 0,
  179. .size = SZ_128K,
  180. .mask_flags = MTD_WRITEABLE,
  181. }, {
  182. .name = "kernel",
  183. .offset = MTDPART_OFS_APPEND,
  184. .size = SZ_4M,
  185. }, {
  186. .name = "initrd",
  187. .offset = MTDPART_OFS_APPEND,
  188. .size = SZ_4M,
  189. }, {
  190. .name = "rootfs",
  191. .offset = MTDPART_OFS_APPEND,
  192. .size = MTDPART_SIZ_FULL,
  193. }
  194. };
  195. static struct platform_nand_data ts78xx_ts_nand_data = {
  196. .chip = {
  197. .nr_chips = 1,
  198. .partitions = ts78xx_ts_nand_parts,
  199. .nr_partitions = ARRAY_SIZE(ts78xx_ts_nand_parts),
  200. .chip_delay = 15,
  201. .bbt_options = NAND_BBT_USE_FLASH,
  202. },
  203. .ctrl = {
  204. /*
  205. * The HW ECC offloading functions, used to give about a 9%
  206. * performance increase for 'dd if=/dev/mtdblockX' and 5% for
  207. * nanddump. This all however was changed by git commit
  208. * e6cf5df1838c28bb060ac45b5585e48e71bbc740 so now there is
  209. * no performance advantage to be had so we no longer bother
  210. */
  211. .cmd_ctrl = ts78xx_ts_nand_cmd_ctrl,
  212. .dev_ready = ts78xx_ts_nand_dev_ready,
  213. .write_buf = ts78xx_ts_nand_write_buf,
  214. .read_buf = ts78xx_ts_nand_read_buf,
  215. },
  216. };
  217. static struct resource ts78xx_ts_nand_resources
  218. = DEFINE_RES_MEM(TS_NAND_DATA, 4);
  219. static struct platform_device ts78xx_ts_nand_device = {
  220. .name = "gen_nand",
  221. .id = -1,
  222. .dev = {
  223. .platform_data = &ts78xx_ts_nand_data,
  224. },
  225. .resource = &ts78xx_ts_nand_resources,
  226. .num_resources = 1,
  227. };
  228. static int ts78xx_ts_nand_load(void)
  229. {
  230. int rc;
  231. if (ts78xx_fpga.supports.ts_nand.init == 0) {
  232. rc = platform_device_register(&ts78xx_ts_nand_device);
  233. if (!rc)
  234. ts78xx_fpga.supports.ts_nand.init = 1;
  235. } else
  236. rc = platform_device_add(&ts78xx_ts_nand_device);
  237. if (rc)
  238. pr_info("NAND could not be registered: %d\n", rc);
  239. return rc;
  240. };
  241. static void ts78xx_ts_nand_unload(void)
  242. {
  243. platform_device_del(&ts78xx_ts_nand_device);
  244. }
  245. /*****************************************************************************
  246. * HW RNG
  247. ****************************************************************************/
  248. #define TS_RNG_DATA (TS78XX_FPGA_REGS_PHYS_BASE | 0x044)
  249. static struct resource ts78xx_ts_rng_resource
  250. = DEFINE_RES_MEM(TS_RNG_DATA, 4);
  251. static struct timeriomem_rng_data ts78xx_ts_rng_data = {
  252. .period = 1000000, /* one second */
  253. };
  254. static struct platform_device ts78xx_ts_rng_device = {
  255. .name = "timeriomem_rng",
  256. .id = -1,
  257. .dev = {
  258. .platform_data = &ts78xx_ts_rng_data,
  259. },
  260. .resource = &ts78xx_ts_rng_resource,
  261. .num_resources = 1,
  262. };
  263. static int ts78xx_ts_rng_load(void)
  264. {
  265. int rc;
  266. if (ts78xx_fpga.supports.ts_rng.init == 0) {
  267. rc = platform_device_register(&ts78xx_ts_rng_device);
  268. if (!rc)
  269. ts78xx_fpga.supports.ts_rng.init = 1;
  270. } else
  271. rc = platform_device_add(&ts78xx_ts_rng_device);
  272. if (rc)
  273. pr_info("RNG could not be registered: %d\n", rc);
  274. return rc;
  275. };
  276. static void ts78xx_ts_rng_unload(void)
  277. {
  278. platform_device_del(&ts78xx_ts_rng_device);
  279. }
  280. /*****************************************************************************
  281. * FPGA 'hotplug' support code
  282. ****************************************************************************/
  283. static void ts78xx_fpga_devices_zero_init(void)
  284. {
  285. ts78xx_fpga.supports.ts_rtc.init = 0;
  286. ts78xx_fpga.supports.ts_nand.init = 0;
  287. ts78xx_fpga.supports.ts_rng.init = 0;
  288. }
  289. static void ts78xx_fpga_supports(void)
  290. {
  291. /* TODO: put this 'table' into ts78xx-fpga.h */
  292. switch (ts78xx_fpga.id) {
  293. case TS7800_REV_1:
  294. case TS7800_REV_2:
  295. case TS7800_REV_3:
  296. case TS7800_REV_4:
  297. case TS7800_REV_5:
  298. case TS7800_REV_6:
  299. case TS7800_REV_7:
  300. case TS7800_REV_8:
  301. case TS7800_REV_9:
  302. ts78xx_fpga.supports.ts_rtc.present = 1;
  303. ts78xx_fpga.supports.ts_nand.present = 1;
  304. ts78xx_fpga.supports.ts_rng.present = 1;
  305. break;
  306. default:
  307. /* enable devices if magic matches */
  308. switch ((ts78xx_fpga.id >> 8) & 0xffffff) {
  309. case TS7800_FPGA_MAGIC:
  310. pr_warn("unrecognised FPGA revision 0x%.2x\n",
  311. ts78xx_fpga.id & 0xff);
  312. ts78xx_fpga.supports.ts_rtc.present = 1;
  313. ts78xx_fpga.supports.ts_nand.present = 1;
  314. ts78xx_fpga.supports.ts_rng.present = 1;
  315. break;
  316. default:
  317. ts78xx_fpga.supports.ts_rtc.present = 0;
  318. ts78xx_fpga.supports.ts_nand.present = 0;
  319. ts78xx_fpga.supports.ts_rng.present = 0;
  320. }
  321. }
  322. }
  323. static int ts78xx_fpga_load_devices(void)
  324. {
  325. int tmp, ret = 0;
  326. if (ts78xx_fpga.supports.ts_rtc.present == 1) {
  327. tmp = ts78xx_ts_rtc_load();
  328. if (tmp)
  329. ts78xx_fpga.supports.ts_rtc.present = 0;
  330. ret |= tmp;
  331. }
  332. if (ts78xx_fpga.supports.ts_nand.present == 1) {
  333. tmp = ts78xx_ts_nand_load();
  334. if (tmp)
  335. ts78xx_fpga.supports.ts_nand.present = 0;
  336. ret |= tmp;
  337. }
  338. if (ts78xx_fpga.supports.ts_rng.present == 1) {
  339. tmp = ts78xx_ts_rng_load();
  340. if (tmp)
  341. ts78xx_fpga.supports.ts_rng.present = 0;
  342. ret |= tmp;
  343. }
  344. return ret;
  345. }
  346. static int ts78xx_fpga_unload_devices(void)
  347. {
  348. int ret = 0;
  349. if (ts78xx_fpga.supports.ts_rtc.present == 1)
  350. ts78xx_ts_rtc_unload();
  351. if (ts78xx_fpga.supports.ts_nand.present == 1)
  352. ts78xx_ts_nand_unload();
  353. if (ts78xx_fpga.supports.ts_rng.present == 1)
  354. ts78xx_ts_rng_unload();
  355. return ret;
  356. }
  357. static int ts78xx_fpga_load(void)
  358. {
  359. ts78xx_fpga.id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
  360. pr_info("FPGA magic=0x%.6x, rev=0x%.2x\n",
  361. (ts78xx_fpga.id >> 8) & 0xffffff,
  362. ts78xx_fpga.id & 0xff);
  363. ts78xx_fpga_supports();
  364. if (ts78xx_fpga_load_devices()) {
  365. ts78xx_fpga.state = -1;
  366. return -EBUSY;
  367. }
  368. return 0;
  369. };
  370. static int ts78xx_fpga_unload(void)
  371. {
  372. unsigned int fpga_id;
  373. fpga_id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
  374. /*
  375. * There does not seem to be a feasible way to block access to the GPIO
  376. * pins from userspace (/dev/mem). This if clause should hopefully warn
  377. * those foolish enough not to follow 'policy' :)
  378. *
  379. * UrJTAG SVN since r1381 can be used to reprogram the FPGA
  380. */
  381. if (ts78xx_fpga.id != fpga_id) {
  382. pr_err("FPGA magic/rev mismatch\n"
  383. "TS-78xx FPGA: was 0x%.6x/%.2x but now 0x%.6x/%.2x\n",
  384. (ts78xx_fpga.id >> 8) & 0xffffff, ts78xx_fpga.id & 0xff,
  385. (fpga_id >> 8) & 0xffffff, fpga_id & 0xff);
  386. ts78xx_fpga.state = -1;
  387. return -EBUSY;
  388. }
  389. if (ts78xx_fpga_unload_devices()) {
  390. ts78xx_fpga.state = -1;
  391. return -EBUSY;
  392. }
  393. return 0;
  394. };
  395. static ssize_t ts78xx_fpga_show(struct kobject *kobj,
  396. struct kobj_attribute *attr, char *buf)
  397. {
  398. if (ts78xx_fpga.state < 0)
  399. return sprintf(buf, "borked\n");
  400. return sprintf(buf, "%s\n", (ts78xx_fpga.state) ? "online" : "offline");
  401. }
  402. static ssize_t ts78xx_fpga_store(struct kobject *kobj,
  403. struct kobj_attribute *attr, const char *buf, size_t n)
  404. {
  405. int value, ret;
  406. if (ts78xx_fpga.state < 0) {
  407. pr_err("FPGA borked, you must powercycle ASAP\n");
  408. return -EBUSY;
  409. }
  410. if (strncmp(buf, "online", sizeof("online") - 1) == 0)
  411. value = 1;
  412. else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
  413. value = 0;
  414. else
  415. return -EINVAL;
  416. if (ts78xx_fpga.state == value)
  417. return n;
  418. ret = (ts78xx_fpga.state == 0)
  419. ? ts78xx_fpga_load()
  420. : ts78xx_fpga_unload();
  421. if (!(ret < 0))
  422. ts78xx_fpga.state = value;
  423. return n;
  424. }
  425. static struct kobj_attribute ts78xx_fpga_attr =
  426. __ATTR(ts78xx_fpga, 0644, ts78xx_fpga_show, ts78xx_fpga_store);
  427. /*****************************************************************************
  428. * General Setup
  429. ****************************************************************************/
  430. static unsigned int ts78xx_mpp_modes[] __initdata = {
  431. MPP0_UNUSED,
  432. MPP1_GPIO, /* JTAG Clock */
  433. MPP2_GPIO, /* JTAG Data In */
  434. MPP3_GPIO, /* Lat ECP2 256 FPGA - PB2B */
  435. MPP4_GPIO, /* JTAG Data Out */
  436. MPP5_GPIO, /* JTAG TMS */
  437. MPP6_GPIO, /* Lat ECP2 256 FPGA - PB31A_CLK4+ */
  438. MPP7_GPIO, /* Lat ECP2 256 FPGA - PB22B */
  439. MPP8_UNUSED,
  440. MPP9_UNUSED,
  441. MPP10_UNUSED,
  442. MPP11_UNUSED,
  443. MPP12_UNUSED,
  444. MPP13_UNUSED,
  445. MPP14_UNUSED,
  446. MPP15_UNUSED,
  447. MPP16_UART,
  448. MPP17_UART,
  449. MPP18_UART,
  450. MPP19_UART,
  451. /*
  452. * MPP[20] PCI Clock Out 1
  453. * MPP[21] PCI Clock Out 0
  454. * MPP[22] Unused
  455. * MPP[23] Unused
  456. * MPP[24] Unused
  457. * MPP[25] Unused
  458. */
  459. 0,
  460. };
  461. static void __init ts78xx_init(void)
  462. {
  463. int ret;
  464. /*
  465. * Setup basic Orion functions. Need to be called early.
  466. */
  467. orion5x_init();
  468. orion5x_mpp_conf(ts78xx_mpp_modes);
  469. /*
  470. * Configure peripherals.
  471. */
  472. orion5x_ehci0_init();
  473. orion5x_ehci1_init();
  474. orion5x_eth_init(&ts78xx_eth_data);
  475. orion5x_sata_init(&ts78xx_sata_data);
  476. orion5x_uart0_init();
  477. orion5x_uart1_init();
  478. orion5x_xor_init();
  479. /* FPGA init */
  480. ts78xx_fpga_devices_zero_init();
  481. ret = ts78xx_fpga_load();
  482. ret = sysfs_create_file(firmware_kobj, &ts78xx_fpga_attr.attr);
  483. if (ret)
  484. pr_err("sysfs_create_file failed: %d\n", ret);
  485. }
  486. MACHINE_START(TS78XX, "Technologic Systems TS-78xx SBC")
  487. /* Maintainer: Alexander Clouter <alex@digriz.org.uk> */
  488. .atag_offset = 0x100,
  489. .nr_irqs = ORION5X_NR_IRQS,
  490. .init_machine = ts78xx_init,
  491. .map_io = ts78xx_map_io,
  492. .init_early = orion5x_init_early,
  493. .init_irq = orion5x_init_irq,
  494. .init_time = orion5x_timer_init,
  495. .restart = orion5x_restart,
  496. MACHINE_END