ts78xx-setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. #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/m48t86.h>
  17. #include <linux/mtd/nand.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 <mach/orion5x.h>
  24. #include "common.h"
  25. #include "mpp.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 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 = 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. 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_VIRT_BASE | 0x808)
  73. #define TS_RTC_DATA (TS78XX_FPGA_REGS_VIRT_BASE | 0x80c)
  74. static unsigned char ts78xx_ts_rtc_readbyte(unsigned long addr)
  75. {
  76. writeb(addr, TS_RTC_CTRL);
  77. return readb(TS_RTC_DATA);
  78. }
  79. static void ts78xx_ts_rtc_writebyte(unsigned char value, unsigned long addr)
  80. {
  81. writeb(addr, TS_RTC_CTRL);
  82. writeb(value, TS_RTC_DATA);
  83. }
  84. static struct m48t86_ops ts78xx_ts_rtc_ops = {
  85. .readbyte = ts78xx_ts_rtc_readbyte,
  86. .writebyte = ts78xx_ts_rtc_writebyte,
  87. };
  88. static struct platform_device ts78xx_ts_rtc_device = {
  89. .name = "rtc-m48t86",
  90. .id = -1,
  91. .dev = {
  92. .platform_data = &ts78xx_ts_rtc_ops,
  93. },
  94. .num_resources = 0,
  95. };
  96. /*
  97. * TS uses some of the user storage space on the RTC chip so see if it is
  98. * present; as it's an optional feature at purchase time and not all boards
  99. * will have it present
  100. *
  101. * I've used the method TS use in their rtc7800.c example for the detection
  102. *
  103. * TODO: track down a guinea pig without an RTC to see if we can work out a
  104. * better RTC detection routine
  105. */
  106. static int ts78xx_ts_rtc_load(void)
  107. {
  108. int rc;
  109. unsigned char tmp_rtc0, tmp_rtc1;
  110. tmp_rtc0 = ts78xx_ts_rtc_readbyte(126);
  111. tmp_rtc1 = ts78xx_ts_rtc_readbyte(127);
  112. ts78xx_ts_rtc_writebyte(0x00, 126);
  113. ts78xx_ts_rtc_writebyte(0x55, 127);
  114. if (ts78xx_ts_rtc_readbyte(127) == 0x55) {
  115. ts78xx_ts_rtc_writebyte(0xaa, 127);
  116. if (ts78xx_ts_rtc_readbyte(127) == 0xaa
  117. && ts78xx_ts_rtc_readbyte(126) == 0x00) {
  118. ts78xx_ts_rtc_writebyte(tmp_rtc0, 126);
  119. ts78xx_ts_rtc_writebyte(tmp_rtc1, 127);
  120. if (ts78xx_fpga.supports.ts_rtc.init == 0) {
  121. rc = platform_device_register(&ts78xx_ts_rtc_device);
  122. if (!rc)
  123. ts78xx_fpga.supports.ts_rtc.init = 1;
  124. } else
  125. rc = platform_device_add(&ts78xx_ts_rtc_device);
  126. return rc;
  127. }
  128. }
  129. return -ENODEV;
  130. };
  131. static void ts78xx_ts_rtc_unload(void)
  132. {
  133. platform_device_del(&ts78xx_ts_rtc_device);
  134. }
  135. /*****************************************************************************
  136. * NAND Flash
  137. ****************************************************************************/
  138. #define TS_NAND_CTRL (TS78XX_FPGA_REGS_VIRT_BASE | 0x800) /* VIRT */
  139. #define TS_NAND_DATA (TS78XX_FPGA_REGS_PHYS_BASE | 0x804) /* PHYS */
  140. /*
  141. * hardware specific access to control-lines
  142. *
  143. * ctrl:
  144. * NAND_NCE: bit 0 -> bit 2
  145. * NAND_CLE: bit 1 -> bit 1
  146. * NAND_ALE: bit 2 -> bit 0
  147. */
  148. static void ts78xx_ts_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  149. unsigned int ctrl)
  150. {
  151. struct nand_chip *this = mtd->priv;
  152. if (ctrl & NAND_CTRL_CHANGE) {
  153. unsigned char bits;
  154. bits = (ctrl & NAND_NCE) << 2;
  155. bits |= ctrl & NAND_CLE;
  156. bits |= (ctrl & NAND_ALE) >> 2;
  157. writeb((readb(TS_NAND_CTRL) & ~0x7) | bits, TS_NAND_CTRL);
  158. }
  159. if (cmd != NAND_CMD_NONE)
  160. writeb(cmd, this->IO_ADDR_W);
  161. }
  162. static int ts78xx_ts_nand_dev_ready(struct mtd_info *mtd)
  163. {
  164. return readb(TS_NAND_CTRL) & 0x20;
  165. }
  166. const char *ts_nand_part_probes[] = { "cmdlinepart", NULL };
  167. static struct mtd_partition ts78xx_ts_nand_parts[] = {
  168. {
  169. .name = "mbr",
  170. .offset = 0,
  171. .size = SZ_128K,
  172. .mask_flags = MTD_WRITEABLE,
  173. }, {
  174. .name = "kernel",
  175. .offset = MTDPART_OFS_APPEND,
  176. .size = SZ_4M,
  177. }, {
  178. .name = "initrd",
  179. .offset = MTDPART_OFS_APPEND,
  180. .size = SZ_4M,
  181. }, {
  182. .name = "rootfs",
  183. .offset = MTDPART_OFS_APPEND,
  184. .size = MTDPART_SIZ_FULL,
  185. }
  186. };
  187. static struct platform_nand_data ts78xx_ts_nand_data = {
  188. .chip = {
  189. .nr_chips = 1,
  190. .part_probe_types = ts_nand_part_probes,
  191. .partitions = ts78xx_ts_nand_parts,
  192. .nr_partitions = ARRAY_SIZE(ts78xx_ts_nand_parts),
  193. .chip_delay = 15,
  194. .options = NAND_USE_FLASH_BBT,
  195. },
  196. .ctrl = {
  197. /*
  198. * The HW ECC offloading functions, used to give about a 9%
  199. * performance increase for 'dd if=/dev/mtdblockX' and 5% for
  200. * nanddump. This all however was changed by git commit
  201. * e6cf5df1838c28bb060ac45b5585e48e71bbc740 so now there is
  202. * no performance advantage to be had so we no longer bother
  203. */
  204. .cmd_ctrl = ts78xx_ts_nand_cmd_ctrl,
  205. .dev_ready = ts78xx_ts_nand_dev_ready,
  206. },
  207. };
  208. static struct resource ts78xx_ts_nand_resources = {
  209. .start = TS_NAND_DATA,
  210. .end = TS_NAND_DATA + 4,
  211. .flags = IORESOURCE_IO,
  212. };
  213. static struct platform_device ts78xx_ts_nand_device = {
  214. .name = "gen_nand",
  215. .id = -1,
  216. .dev = {
  217. .platform_data = &ts78xx_ts_nand_data,
  218. },
  219. .resource = &ts78xx_ts_nand_resources,
  220. .num_resources = 1,
  221. };
  222. static int ts78xx_ts_nand_load(void)
  223. {
  224. int rc;
  225. if (ts78xx_fpga.supports.ts_nand.init == 0) {
  226. rc = platform_device_register(&ts78xx_ts_nand_device);
  227. if (!rc)
  228. ts78xx_fpga.supports.ts_nand.init = 1;
  229. } else
  230. rc = platform_device_add(&ts78xx_ts_nand_device);
  231. return rc;
  232. };
  233. static void ts78xx_ts_nand_unload(void)
  234. {
  235. platform_device_del(&ts78xx_ts_nand_device);
  236. }
  237. /*****************************************************************************
  238. * HW RNG
  239. ****************************************************************************/
  240. #define TS_RNG_DATA (TS78XX_FPGA_REGS_PHYS_BASE | 0x044)
  241. static struct resource ts78xx_ts_rng_resource = {
  242. .flags = IORESOURCE_MEM,
  243. .start = TS_RNG_DATA,
  244. .end = TS_RNG_DATA + 4 - 1,
  245. };
  246. static struct timeriomem_rng_data ts78xx_ts_rng_data = {
  247. .period = 1000000, /* one second */
  248. };
  249. static struct platform_device ts78xx_ts_rng_device = {
  250. .name = "timeriomem_rng",
  251. .id = -1,
  252. .dev = {
  253. .platform_data = &ts78xx_ts_rng_data,
  254. },
  255. .resource = &ts78xx_ts_rng_resource,
  256. .num_resources = 1,
  257. };
  258. static int ts78xx_ts_rng_load(void)
  259. {
  260. int rc;
  261. if (ts78xx_fpga.supports.ts_rng.init == 0) {
  262. rc = platform_device_register(&ts78xx_ts_rng_device);
  263. if (!rc)
  264. ts78xx_fpga.supports.ts_rng.init = 1;
  265. } else
  266. rc = platform_device_add(&ts78xx_ts_rng_device);
  267. return rc;
  268. };
  269. static void ts78xx_ts_rng_unload(void)
  270. {
  271. platform_device_del(&ts78xx_ts_rng_device);
  272. }
  273. /*****************************************************************************
  274. * FPGA 'hotplug' support code
  275. ****************************************************************************/
  276. static void ts78xx_fpga_devices_zero_init(void)
  277. {
  278. ts78xx_fpga.supports.ts_rtc.init = 0;
  279. ts78xx_fpga.supports.ts_nand.init = 0;
  280. ts78xx_fpga.supports.ts_rng.init = 0;
  281. }
  282. static void ts78xx_fpga_supports(void)
  283. {
  284. /* TODO: put this 'table' into ts78xx-fpga.h */
  285. switch (ts78xx_fpga.id) {
  286. case TS7800_REV_1:
  287. case TS7800_REV_2:
  288. case TS7800_REV_3:
  289. case TS7800_REV_4:
  290. case TS7800_REV_5:
  291. ts78xx_fpga.supports.ts_rtc.present = 1;
  292. ts78xx_fpga.supports.ts_nand.present = 1;
  293. ts78xx_fpga.supports.ts_rng.present = 1;
  294. break;
  295. default:
  296. ts78xx_fpga.supports.ts_rtc.present = 0;
  297. ts78xx_fpga.supports.ts_nand.present = 0;
  298. ts78xx_fpga.supports.ts_rng.present = 0;
  299. }
  300. }
  301. static int ts78xx_fpga_load_devices(void)
  302. {
  303. int tmp, ret = 0;
  304. if (ts78xx_fpga.supports.ts_rtc.present == 1) {
  305. tmp = ts78xx_ts_rtc_load();
  306. if (tmp) {
  307. printk(KERN_INFO "TS-78xx: RTC not registered\n");
  308. ts78xx_fpga.supports.ts_rtc.present = 0;
  309. }
  310. ret |= tmp;
  311. }
  312. if (ts78xx_fpga.supports.ts_nand.present == 1) {
  313. tmp = ts78xx_ts_nand_load();
  314. if (tmp) {
  315. printk(KERN_INFO "TS-78xx: NAND not registered\n");
  316. ts78xx_fpga.supports.ts_nand.present = 0;
  317. }
  318. ret |= tmp;
  319. }
  320. if (ts78xx_fpga.supports.ts_rng.present == 1) {
  321. tmp = ts78xx_ts_rng_load();
  322. if (tmp) {
  323. printk(KERN_INFO "TS-78xx: RNG not registered\n");
  324. ts78xx_fpga.supports.ts_rng.present = 0;
  325. }
  326. ret |= tmp;
  327. }
  328. return ret;
  329. }
  330. static int ts78xx_fpga_unload_devices(void)
  331. {
  332. int ret = 0;
  333. if (ts78xx_fpga.supports.ts_rtc.present == 1)
  334. ts78xx_ts_rtc_unload();
  335. if (ts78xx_fpga.supports.ts_nand.present == 1)
  336. ts78xx_ts_nand_unload();
  337. if (ts78xx_fpga.supports.ts_rng.present == 1)
  338. ts78xx_ts_rng_unload();
  339. return ret;
  340. }
  341. static int ts78xx_fpga_load(void)
  342. {
  343. ts78xx_fpga.id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
  344. printk(KERN_INFO "TS-78xx FPGA: magic=0x%.6x, rev=0x%.2x\n",
  345. (ts78xx_fpga.id >> 8) & 0xffffff,
  346. ts78xx_fpga.id & 0xff);
  347. ts78xx_fpga_supports();
  348. if (ts78xx_fpga_load_devices()) {
  349. ts78xx_fpga.state = -1;
  350. return -EBUSY;
  351. }
  352. return 0;
  353. };
  354. static int ts78xx_fpga_unload(void)
  355. {
  356. unsigned int fpga_id;
  357. fpga_id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
  358. /*
  359. * There does not seem to be a feasible way to block access to the GPIO
  360. * pins from userspace (/dev/mem). This if clause should hopefully warn
  361. * those foolish enough not to follow 'policy' :)
  362. *
  363. * UrJTAG SVN since r1381 can be used to reprogram the FPGA
  364. */
  365. if (ts78xx_fpga.id != fpga_id) {
  366. printk(KERN_ERR "TS-78xx FPGA: magic/rev mismatch\n"
  367. "TS-78xx FPGA: was 0x%.6x/%.2x but now 0x%.6x/%.2x\n",
  368. (ts78xx_fpga.id >> 8) & 0xffffff, ts78xx_fpga.id & 0xff,
  369. (fpga_id >> 8) & 0xffffff, fpga_id & 0xff);
  370. ts78xx_fpga.state = -1;
  371. return -EBUSY;
  372. }
  373. if (ts78xx_fpga_unload_devices()) {
  374. ts78xx_fpga.state = -1;
  375. return -EBUSY;
  376. }
  377. return 0;
  378. };
  379. static ssize_t ts78xx_fpga_show(struct kobject *kobj,
  380. struct kobj_attribute *attr, char *buf)
  381. {
  382. if (ts78xx_fpga.state < 0)
  383. return sprintf(buf, "borked\n");
  384. return sprintf(buf, "%s\n", (ts78xx_fpga.state) ? "online" : "offline");
  385. }
  386. static ssize_t ts78xx_fpga_store(struct kobject *kobj,
  387. struct kobj_attribute *attr, const char *buf, size_t n)
  388. {
  389. int value, ret;
  390. if (ts78xx_fpga.state < 0) {
  391. printk(KERN_ERR "TS-78xx FPGA: borked, you must powercycle asap\n");
  392. return -EBUSY;
  393. }
  394. if (strncmp(buf, "online", sizeof("online") - 1) == 0)
  395. value = 1;
  396. else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
  397. value = 0;
  398. else {
  399. printk(KERN_ERR "ts78xx_fpga_store: Invalid value\n");
  400. return -EINVAL;
  401. }
  402. if (ts78xx_fpga.state == value)
  403. return n;
  404. ret = (ts78xx_fpga.state == 0)
  405. ? ts78xx_fpga_load()
  406. : ts78xx_fpga_unload();
  407. if (!(ret < 0))
  408. ts78xx_fpga.state = value;
  409. return n;
  410. }
  411. static struct kobj_attribute ts78xx_fpga_attr =
  412. __ATTR(ts78xx_fpga, 0644, ts78xx_fpga_show, ts78xx_fpga_store);
  413. /*****************************************************************************
  414. * General Setup
  415. ****************************************************************************/
  416. static struct orion5x_mpp_mode ts78xx_mpp_modes[] __initdata = {
  417. { 0, MPP_UNUSED },
  418. { 1, MPP_GPIO }, /* JTAG Clock */
  419. { 2, MPP_GPIO }, /* JTAG Data In */
  420. { 3, MPP_GPIO }, /* Lat ECP2 256 FPGA - PB2B */
  421. { 4, MPP_GPIO }, /* JTAG Data Out */
  422. { 5, MPP_GPIO }, /* JTAG TMS */
  423. { 6, MPP_GPIO }, /* Lat ECP2 256 FPGA - PB31A_CLK4+ */
  424. { 7, MPP_GPIO }, /* Lat ECP2 256 FPGA - PB22B */
  425. { 8, MPP_UNUSED },
  426. { 9, MPP_UNUSED },
  427. { 10, MPP_UNUSED },
  428. { 11, MPP_UNUSED },
  429. { 12, MPP_UNUSED },
  430. { 13, MPP_UNUSED },
  431. { 14, MPP_UNUSED },
  432. { 15, MPP_UNUSED },
  433. { 16, MPP_UART },
  434. { 17, MPP_UART },
  435. { 18, MPP_UART },
  436. { 19, MPP_UART },
  437. /*
  438. * MPP[20] PCI Clock Out 1
  439. * MPP[21] PCI Clock Out 0
  440. * MPP[22] Unused
  441. * MPP[23] Unused
  442. * MPP[24] Unused
  443. * MPP[25] Unused
  444. */
  445. { -1 },
  446. };
  447. static void __init ts78xx_init(void)
  448. {
  449. int ret;
  450. /*
  451. * Setup basic Orion functions. Need to be called early.
  452. */
  453. orion5x_init();
  454. orion5x_mpp_conf(ts78xx_mpp_modes);
  455. /*
  456. * Configure peripherals.
  457. */
  458. orion5x_ehci0_init();
  459. orion5x_ehci1_init();
  460. orion5x_eth_init(&ts78xx_eth_data);
  461. orion5x_sata_init(&ts78xx_sata_data);
  462. orion5x_uart0_init();
  463. orion5x_uart1_init();
  464. orion5x_xor_init();
  465. /* FPGA init */
  466. ts78xx_fpga_devices_zero_init();
  467. ret = ts78xx_fpga_load();
  468. ret = sysfs_create_file(power_kobj, &ts78xx_fpga_attr.attr);
  469. if (ret)
  470. printk(KERN_ERR "sysfs_create_file failed: %d\n", ret);
  471. }
  472. MACHINE_START(TS78XX, "Technologic Systems TS-78xx SBC")
  473. /* Maintainer: Alexander Clouter <alex@digriz.org.uk> */
  474. .phys_io = ORION5X_REGS_PHYS_BASE,
  475. .io_pg_offst = ((ORION5X_REGS_VIRT_BASE) >> 18) & 0xFFFC,
  476. .boot_params = 0x00000100,
  477. .init_machine = ts78xx_init,
  478. .map_io = ts78xx_map_io,
  479. .init_irq = orion5x_init_irq,
  480. .timer = &orion5x_timer,
  481. MACHINE_END