ts78xx-setup.c 14 KB

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