ts78xx-setup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. .part_probe_types = ts_nand_part_probes,
  190. .partitions = ts78xx_ts_nand_parts,
  191. .nr_partitions = ARRAY_SIZE(ts78xx_ts_nand_parts),
  192. .chip_delay = 15,
  193. .options = NAND_USE_FLASH_BBT,
  194. },
  195. .ctrl = {
  196. /*
  197. * The HW ECC offloading functions, used to give about a 9%
  198. * performance increase for 'dd if=/dev/mtdblockX' and 5% for
  199. * nanddump. This all however was changed by git commit
  200. * e6cf5df1838c28bb060ac45b5585e48e71bbc740 so now there is
  201. * no performance advantage to be had so we no longer bother
  202. */
  203. .cmd_ctrl = ts78xx_ts_nand_cmd_ctrl,
  204. .dev_ready = ts78xx_ts_nand_dev_ready,
  205. },
  206. };
  207. static struct resource ts78xx_ts_nand_resources = {
  208. .start = TS_NAND_DATA,
  209. .end = TS_NAND_DATA + 4,
  210. .flags = IORESOURCE_IO,
  211. };
  212. static struct platform_device ts78xx_ts_nand_device = {
  213. .name = "gen_nand",
  214. .id = -1,
  215. .dev = {
  216. .platform_data = &ts78xx_ts_nand_data,
  217. },
  218. .resource = &ts78xx_ts_nand_resources,
  219. .num_resources = 1,
  220. };
  221. static int ts78xx_ts_nand_load(void)
  222. {
  223. int rc;
  224. if (ts78xx_fpga.supports.ts_nand.init == 0) {
  225. rc = platform_device_register(&ts78xx_ts_nand_device);
  226. if (!rc)
  227. ts78xx_fpga.supports.ts_nand.init = 1;
  228. } else
  229. rc = platform_device_add(&ts78xx_ts_nand_device);
  230. return rc;
  231. };
  232. static void ts78xx_ts_nand_unload(void)
  233. {
  234. platform_device_del(&ts78xx_ts_nand_device);
  235. }
  236. /*****************************************************************************
  237. * HW RNG
  238. ****************************************************************************/
  239. #define TS_RNG_DATA (TS78XX_FPGA_REGS_PHYS_BASE | 0x044)
  240. static struct resource ts78xx_ts_rng_resource = {
  241. .flags = IORESOURCE_MEM,
  242. .start = TS_RNG_DATA,
  243. .end = TS_RNG_DATA + 4 - 1,
  244. };
  245. static struct timeriomem_rng_data ts78xx_ts_rng_data = {
  246. .period = 1000000, /* one second */
  247. };
  248. static struct platform_device ts78xx_ts_rng_device = {
  249. .name = "timeriomem_rng",
  250. .id = -1,
  251. .dev = {
  252. .platform_data = &ts78xx_ts_rng_data,
  253. },
  254. .resource = &ts78xx_ts_rng_resource,
  255. .num_resources = 1,
  256. };
  257. static int ts78xx_ts_rng_load(void)
  258. {
  259. int rc;
  260. if (ts78xx_fpga.supports.ts_rng.init == 0) {
  261. rc = platform_device_register(&ts78xx_ts_rng_device);
  262. if (!rc)
  263. ts78xx_fpga.supports.ts_rng.init = 1;
  264. } else
  265. rc = platform_device_add(&ts78xx_ts_rng_device);
  266. return rc;
  267. };
  268. static void ts78xx_ts_rng_unload(void)
  269. {
  270. platform_device_del(&ts78xx_ts_rng_device);
  271. }
  272. /*****************************************************************************
  273. * FPGA 'hotplug' support code
  274. ****************************************************************************/
  275. static void ts78xx_fpga_devices_zero_init(void)
  276. {
  277. ts78xx_fpga.supports.ts_rtc.init = 0;
  278. ts78xx_fpga.supports.ts_nand.init = 0;
  279. ts78xx_fpga.supports.ts_rng.init = 0;
  280. }
  281. static void ts78xx_fpga_supports(void)
  282. {
  283. /* TODO: put this 'table' into ts78xx-fpga.h */
  284. switch (ts78xx_fpga.id) {
  285. case TS7800_REV_1:
  286. case TS7800_REV_2:
  287. case TS7800_REV_3:
  288. case TS7800_REV_4:
  289. case TS7800_REV_5:
  290. ts78xx_fpga.supports.ts_rtc.present = 1;
  291. ts78xx_fpga.supports.ts_nand.present = 1;
  292. ts78xx_fpga.supports.ts_rng.present = 1;
  293. break;
  294. default:
  295. ts78xx_fpga.supports.ts_rtc.present = 0;
  296. ts78xx_fpga.supports.ts_nand.present = 0;
  297. ts78xx_fpga.supports.ts_rng.present = 0;
  298. }
  299. }
  300. static int ts78xx_fpga_load_devices(void)
  301. {
  302. int tmp, ret = 0;
  303. if (ts78xx_fpga.supports.ts_rtc.present == 1) {
  304. tmp = ts78xx_ts_rtc_load();
  305. if (tmp) {
  306. printk(KERN_INFO "TS-78xx: RTC not registered\n");
  307. ts78xx_fpga.supports.ts_rtc.present = 0;
  308. }
  309. ret |= tmp;
  310. }
  311. if (ts78xx_fpga.supports.ts_nand.present == 1) {
  312. tmp = ts78xx_ts_nand_load();
  313. if (tmp) {
  314. printk(KERN_INFO "TS-78xx: NAND not registered\n");
  315. ts78xx_fpga.supports.ts_nand.present = 0;
  316. }
  317. ret |= tmp;
  318. }
  319. if (ts78xx_fpga.supports.ts_rng.present == 1) {
  320. tmp = ts78xx_ts_rng_load();
  321. if (tmp) {
  322. printk(KERN_INFO "TS-78xx: RNG not registered\n");
  323. ts78xx_fpga.supports.ts_rng.present = 0;
  324. }
  325. ret |= tmp;
  326. }
  327. return ret;
  328. }
  329. static int ts78xx_fpga_unload_devices(void)
  330. {
  331. int ret = 0;
  332. if (ts78xx_fpga.supports.ts_rtc.present == 1)
  333. ts78xx_ts_rtc_unload();
  334. if (ts78xx_fpga.supports.ts_nand.present == 1)
  335. ts78xx_ts_nand_unload();
  336. if (ts78xx_fpga.supports.ts_rng.present == 1)
  337. ts78xx_ts_rng_unload();
  338. return ret;
  339. }
  340. static int ts78xx_fpga_load(void)
  341. {
  342. ts78xx_fpga.id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
  343. printk(KERN_INFO "TS-78xx FPGA: magic=0x%.6x, rev=0x%.2x\n",
  344. (ts78xx_fpga.id >> 8) & 0xffffff,
  345. ts78xx_fpga.id & 0xff);
  346. ts78xx_fpga_supports();
  347. if (ts78xx_fpga_load_devices()) {
  348. ts78xx_fpga.state = -1;
  349. return -EBUSY;
  350. }
  351. return 0;
  352. };
  353. static int ts78xx_fpga_unload(void)
  354. {
  355. unsigned int fpga_id;
  356. fpga_id = readl(TS78XX_FPGA_REGS_VIRT_BASE);
  357. /*
  358. * There does not seem to be a feasible way to block access to the GPIO
  359. * pins from userspace (/dev/mem). This if clause should hopefully warn
  360. * those foolish enough not to follow 'policy' :)
  361. *
  362. * UrJTAG SVN since r1381 can be used to reprogram the FPGA
  363. */
  364. if (ts78xx_fpga.id != fpga_id) {
  365. printk(KERN_ERR "TS-78xx FPGA: magic/rev mismatch\n"
  366. "TS-78xx FPGA: was 0x%.6x/%.2x but now 0x%.6x/%.2x\n",
  367. (ts78xx_fpga.id >> 8) & 0xffffff, ts78xx_fpga.id & 0xff,
  368. (fpga_id >> 8) & 0xffffff, fpga_id & 0xff);
  369. ts78xx_fpga.state = -1;
  370. return -EBUSY;
  371. }
  372. if (ts78xx_fpga_unload_devices()) {
  373. ts78xx_fpga.state = -1;
  374. return -EBUSY;
  375. }
  376. return 0;
  377. };
  378. static ssize_t ts78xx_fpga_show(struct kobject *kobj,
  379. struct kobj_attribute *attr, char *buf)
  380. {
  381. if (ts78xx_fpga.state < 0)
  382. return sprintf(buf, "borked\n");
  383. return sprintf(buf, "%s\n", (ts78xx_fpga.state) ? "online" : "offline");
  384. }
  385. static ssize_t ts78xx_fpga_store(struct kobject *kobj,
  386. struct kobj_attribute *attr, const char *buf, size_t n)
  387. {
  388. int value, ret;
  389. if (ts78xx_fpga.state < 0) {
  390. printk(KERN_ERR "TS-78xx FPGA: borked, you must powercycle asap\n");
  391. return -EBUSY;
  392. }
  393. if (strncmp(buf, "online", sizeof("online") - 1) == 0)
  394. value = 1;
  395. else if (strncmp(buf, "offline", sizeof("offline") - 1) == 0)
  396. value = 0;
  397. else {
  398. printk(KERN_ERR "ts78xx_fpga_store: Invalid value\n");
  399. return -EINVAL;
  400. }
  401. if (ts78xx_fpga.state == value)
  402. return n;
  403. ret = (ts78xx_fpga.state == 0)
  404. ? ts78xx_fpga_load()
  405. : ts78xx_fpga_unload();
  406. if (!(ret < 0))
  407. ts78xx_fpga.state = value;
  408. return n;
  409. }
  410. static struct kobj_attribute ts78xx_fpga_attr =
  411. __ATTR(ts78xx_fpga, 0644, ts78xx_fpga_show, ts78xx_fpga_store);
  412. /*****************************************************************************
  413. * General Setup
  414. ****************************************************************************/
  415. static struct orion5x_mpp_mode ts78xx_mpp_modes[] __initdata = {
  416. { 0, MPP_UNUSED },
  417. { 1, MPP_GPIO }, /* JTAG Clock */
  418. { 2, MPP_GPIO }, /* JTAG Data In */
  419. { 3, MPP_GPIO }, /* Lat ECP2 256 FPGA - PB2B */
  420. { 4, MPP_GPIO }, /* JTAG Data Out */
  421. { 5, MPP_GPIO }, /* JTAG TMS */
  422. { 6, MPP_GPIO }, /* Lat ECP2 256 FPGA - PB31A_CLK4+ */
  423. { 7, MPP_GPIO }, /* Lat ECP2 256 FPGA - PB22B */
  424. { 8, MPP_UNUSED },
  425. { 9, MPP_UNUSED },
  426. { 10, MPP_UNUSED },
  427. { 11, MPP_UNUSED },
  428. { 12, MPP_UNUSED },
  429. { 13, MPP_UNUSED },
  430. { 14, MPP_UNUSED },
  431. { 15, MPP_UNUSED },
  432. { 16, MPP_UART },
  433. { 17, MPP_UART },
  434. { 18, MPP_UART },
  435. { 19, MPP_UART },
  436. /*
  437. * MPP[20] PCI Clock Out 1
  438. * MPP[21] PCI Clock Out 0
  439. * MPP[22] Unused
  440. * MPP[23] Unused
  441. * MPP[24] Unused
  442. * MPP[25] Unused
  443. */
  444. { -1 },
  445. };
  446. static void __init ts78xx_init(void)
  447. {
  448. int ret;
  449. /*
  450. * Setup basic Orion functions. Need to be called early.
  451. */
  452. orion5x_init();
  453. orion5x_mpp_conf(ts78xx_mpp_modes);
  454. /*
  455. * Configure peripherals.
  456. */
  457. orion5x_ehci0_init();
  458. orion5x_ehci1_init();
  459. orion5x_eth_init(&ts78xx_eth_data);
  460. orion5x_sata_init(&ts78xx_sata_data);
  461. orion5x_uart0_init();
  462. orion5x_uart1_init();
  463. orion5x_xor_init();
  464. /* FPGA init */
  465. ts78xx_fpga_devices_zero_init();
  466. ret = ts78xx_fpga_load();
  467. ret = sysfs_create_file(power_kobj, &ts78xx_fpga_attr.attr);
  468. if (ret)
  469. printk(KERN_ERR "sysfs_create_file failed: %d\n", ret);
  470. }
  471. MACHINE_START(TS78XX, "Technologic Systems TS-78xx SBC")
  472. /* Maintainer: Alexander Clouter <alex@digriz.org.uk> */
  473. .phys_io = ORION5X_REGS_PHYS_BASE,
  474. .io_pg_offst = ((ORION5X_REGS_VIRT_BASE) >> 18) & 0xFFFC,
  475. .boot_params = 0x00000100,
  476. .init_machine = ts78xx_init,
  477. .map_io = ts78xx_map_io,
  478. .init_irq = orion5x_init_irq,
  479. .timer = &orion5x_timer,
  480. MACHINE_END