adm5120-flash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Platform driver for NOR flash devices on ADM5120 based boards
  3. *
  4. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This file was derived from: drivers/mtd/map/physmap.c
  7. * Copyright (C) 2003 MontaVista Software Inc.
  8. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/io.h>
  21. #include <linux/device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/map.h>
  25. #include <linux/mtd/partitions.h>
  26. #include <asm/mach-adm5120/adm5120_defs.h>
  27. #include <asm/mach-adm5120/adm5120_switch.h>
  28. #include <asm/mach-adm5120/adm5120_mpmc.h>
  29. #include <asm/mach-adm5120/adm5120_platform.h>
  30. #define DRV_NAME "adm5120-flash"
  31. #define DRV_DESC "ADM5120 flash MAP driver"
  32. #define MAX_PARSED_PARTS 8
  33. #ifdef ADM5120_FLASH_DEBUG
  34. #define MAP_DBG(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
  35. #else
  36. #define MAP_DBG(m, f, a...) do {} while (0)
  37. #endif
  38. #define MAP_ERR(m, f, a...) printk(KERN_ERR "%s: " f, (m->name) , ## a)
  39. #define MAP_INFO(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
  40. struct adm5120_map_info {
  41. struct map_info map;
  42. void (*switch_bank)(unsigned);
  43. unsigned long window_size;
  44. };
  45. struct adm5120_flash_info {
  46. struct mtd_info *mtd;
  47. struct resource *res;
  48. struct platform_device *dev;
  49. struct adm5120_map_info amap;
  50. };
  51. struct flash_desc {
  52. u32 phys;
  53. u32 srs_shift;
  54. };
  55. /*
  56. * Globals
  57. */
  58. static DEFINE_SPINLOCK(adm5120_flash_spin);
  59. #define FLASH_LOCK() spin_lock(&adm5120_flash_spin)
  60. #define FLASH_UNLOCK() spin_unlock(&adm5120_flash_spin)
  61. static u32 flash_bankwidths[4] = { 1, 2, 4, 0 };
  62. static u32 flash_sizes[8] = {
  63. 0, 512*1024, 1024*1024, 2*1024*1024,
  64. 4*1024*1024, 0, 0, 0
  65. };
  66. static struct flash_desc flash_descs[2] = {
  67. {
  68. .phys = ADM5120_SRAM0_BASE,
  69. .srs_shift = MEMCTRL_SRS0_SHIFT,
  70. }, {
  71. .phys = ADM5120_SRAM1_BASE,
  72. .srs_shift = MEMCTRL_SRS1_SHIFT,
  73. }
  74. };
  75. static const char const *probe_types[] = {
  76. "cfi_probe",
  77. "jedec_probe",
  78. "map_rom",
  79. NULL
  80. };
  81. static const char const *parse_types[] = {
  82. "cmdlinepart",
  83. #ifdef CONFIG_MTD_REDBOOT_PARTS
  84. "RedBoot",
  85. #endif
  86. #ifdef CONFIG_MTD_MYLOADER_PARTS
  87. "MyLoader",
  88. #endif
  89. NULL,
  90. };
  91. #define BANK_SIZE (2<<20)
  92. #define BANK_SIZE_MAX (4<<20)
  93. #define BANK_OFFS_MASK (BANK_SIZE-1)
  94. #define BANK_START_MASK (~BANK_OFFS_MASK)
  95. static inline struct adm5120_map_info *map_to_amap(struct map_info *map)
  96. {
  97. return (struct adm5120_map_info *)map;
  98. }
  99. static void adm5120_flash_switchbank(struct map_info *map,
  100. unsigned long ofs)
  101. {
  102. struct adm5120_map_info *amap = map_to_amap(map);
  103. unsigned bank;
  104. if (amap->switch_bank == NULL)
  105. return;
  106. bank = (ofs & BANK_START_MASK) >> 21;
  107. if (bank > 1)
  108. BUG();
  109. MAP_DBG(map, "switching to bank %u, ofs=%lX\n", bank, ofs);
  110. amap->switch_bank(bank);
  111. }
  112. static map_word adm5120_flash_read(struct map_info *map, unsigned long ofs)
  113. {
  114. struct adm5120_map_info *amap = map_to_amap(map);
  115. map_word ret;
  116. MAP_DBG(map, "reading from ofs %lX\n", ofs);
  117. if (ofs >= amap->window_size)
  118. return map_word_ff(map);
  119. FLASH_LOCK();
  120. adm5120_flash_switchbank(map, ofs);
  121. ret = inline_map_read(map, (ofs & (amap->window_size-1)));
  122. FLASH_UNLOCK();
  123. return ret;
  124. }
  125. static void adm5120_flash_write(struct map_info *map, const map_word datum,
  126. unsigned long ofs)
  127. {
  128. struct adm5120_map_info *amap = map_to_amap(map);
  129. MAP_DBG(map, "writing to ofs %lX\n", ofs);
  130. if (ofs > amap->window_size)
  131. return;
  132. FLASH_LOCK();
  133. adm5120_flash_switchbank(map, ofs);
  134. inline_map_write(map, datum, (ofs & (amap->window_size-1)));
  135. FLASH_UNLOCK();
  136. }
  137. static void adm5120_flash_copy_from(struct map_info *map, void *to,
  138. unsigned long from, ssize_t len)
  139. {
  140. struct adm5120_map_info *amap = map_to_amap(map);
  141. char *p;
  142. ssize_t t;
  143. MAP_DBG(map, "copy_from, to=%lX, from=%lX, len=%lX\n",
  144. (unsigned long)to, from, (unsigned long)len);
  145. if (from > amap->window_size)
  146. return;
  147. p = (char *)to;
  148. while (len > 0) {
  149. t = len;
  150. if ((from < BANK_SIZE) && ((from+len) > BANK_SIZE))
  151. t = BANK_SIZE-from;
  152. FLASH_LOCK();
  153. MAP_DBG(map, "copying %lu byte(s) from %lX to %lX\n",
  154. (unsigned long)t, (from & (amap->window_size-1)),
  155. (unsigned long)p);
  156. adm5120_flash_switchbank(map, from);
  157. inline_map_copy_from(map, p, (from & (amap->window_size-1)), t);
  158. FLASH_UNLOCK();
  159. p += t;
  160. from += t;
  161. len -= t;
  162. }
  163. }
  164. static int adm5120_flash_initres(struct adm5120_flash_info *info)
  165. {
  166. struct map_info *map = &info->amap.map;
  167. int err = 0;
  168. info->res = request_mem_region(map->phys, info->amap.window_size,
  169. map->name);
  170. if (info->res == NULL) {
  171. MAP_ERR(map, "could not reserve memory region\n");
  172. err = -ENOMEM;
  173. goto out;
  174. }
  175. map->virt = ioremap_nocache(map->phys, info->amap.window_size);
  176. if (map->virt == NULL) {
  177. MAP_ERR(map, "failed to ioremap flash region\n");
  178. err = -ENOMEM;
  179. goto out;
  180. }
  181. out:
  182. return err;
  183. }
  184. static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
  185. struct platform_device *dev)
  186. {
  187. struct map_info *map = &info->amap.map;
  188. struct adm5120_flash_platform_data *pdata = dev->dev.platform_data;
  189. struct flash_desc *fdesc;
  190. u32 t = 0;
  191. map->name = dev_name(&dev->dev);
  192. if (dev->id > 1) {
  193. MAP_ERR(map, "invalid flash id\n");
  194. goto err_out;
  195. }
  196. fdesc = &flash_descs[dev->id];
  197. if (pdata)
  198. info->amap.window_size = pdata->window_size;
  199. if (info->amap.window_size == 0) {
  200. /* get memory window size */
  201. t = SW_READ_REG(SWITCH_REG_MEMCTRL) >> fdesc->srs_shift;
  202. t &= MEMCTRL_SRS_MASK;
  203. info->amap.window_size = flash_sizes[t];
  204. }
  205. if (info->amap.window_size == 0) {
  206. MAP_ERR(map, "unable to determine window size\n");
  207. goto err_out;
  208. }
  209. /* get flash bus width */
  210. switch (dev->id) {
  211. case 0:
  212. t = MPMC_READ_REG(SC1) & SC_MW_MASK;
  213. break;
  214. case 1:
  215. t = MPMC_READ_REG(SC0) & SC_MW_MASK;
  216. break;
  217. }
  218. map->bankwidth = flash_bankwidths[t];
  219. if (map->bankwidth == 0) {
  220. MAP_ERR(map, "invalid bus width detected\n");
  221. goto err_out;
  222. }
  223. map->phys = fdesc->phys;
  224. map->size = BANK_SIZE_MAX;
  225. simple_map_init(map);
  226. map->read = adm5120_flash_read;
  227. map->write = adm5120_flash_write;
  228. map->copy_from = adm5120_flash_copy_from;
  229. if (pdata) {
  230. map->set_vpp = pdata->set_vpp;
  231. info->amap.switch_bank = pdata->switch_bank;
  232. }
  233. info->dev = dev;
  234. MAP_INFO(map, "probing at 0x%lX, size:%ldKiB, width:%d bits\n",
  235. (unsigned long)map->phys,
  236. (unsigned long)info->amap.window_size >> 10,
  237. map->bankwidth*8);
  238. return 0;
  239. err_out:
  240. return -ENODEV;
  241. }
  242. static void adm5120_flash_initbanks(struct adm5120_flash_info *info)
  243. {
  244. struct map_info *map = &info->amap.map;
  245. if (info->mtd->size <= BANK_SIZE)
  246. /* no bank switching needed */
  247. return;
  248. if (info->amap.switch_bank) {
  249. info->amap.window_size = info->mtd->size;
  250. return;
  251. }
  252. MAP_ERR(map, "reduce visibility from %ldKiB to %ldKiB\n",
  253. (unsigned long)map->size >> 10,
  254. (unsigned long)info->mtd->size >> 10);
  255. info->mtd->size = info->amap.window_size;
  256. }
  257. static int adm5120_flash_remove(struct platform_device *dev)
  258. {
  259. struct adm5120_flash_info *info;
  260. info = platform_get_drvdata(dev);
  261. if (info == NULL)
  262. return 0;
  263. platform_set_drvdata(dev, NULL);
  264. if (info->mtd != NULL) {
  265. mtd_device_unregister(info->mtd);
  266. map_destroy(info->mtd);
  267. }
  268. if (info->amap.map.virt != NULL)
  269. iounmap(info->amap.map.virt);
  270. if (info->res != NULL) {
  271. release_resource(info->res);
  272. kfree(info->res);
  273. }
  274. return 0;
  275. }
  276. static int adm5120_flash_probe(struct platform_device *dev)
  277. {
  278. struct adm5120_flash_platform_data *pdata;
  279. struct adm5120_flash_info *info;
  280. struct map_info *map;
  281. const char **probe_type;
  282. int err;
  283. pdata = dev->dev.platform_data;
  284. if (!pdata) {
  285. dev_err(&dev->dev, "no platform data\n");
  286. return -EINVAL;
  287. }
  288. info = kzalloc(sizeof(*info), GFP_KERNEL);
  289. if (info == NULL) {
  290. err = -ENOMEM;
  291. goto err_out;
  292. }
  293. platform_set_drvdata(dev, info);
  294. err = adm5120_flash_initinfo(info, dev);
  295. if (err)
  296. goto err_out;
  297. err = adm5120_flash_initres(info);
  298. if (err)
  299. goto err_out;
  300. map = &info->amap.map;
  301. for (probe_type = probe_types; info->mtd == NULL && *probe_type != NULL;
  302. probe_type++)
  303. info->mtd = do_map_probe(*probe_type, map);
  304. if (info->mtd == NULL) {
  305. MAP_ERR(map, "map_probe failed\n");
  306. err = -ENXIO;
  307. goto err_out;
  308. }
  309. adm5120_flash_initbanks(info);
  310. if (info->mtd->size < info->amap.window_size) {
  311. /* readjust resources */
  312. iounmap(map->virt);
  313. release_resource(info->res);
  314. kfree(info->res);
  315. info->amap.window_size = info->mtd->size;
  316. map->size = info->mtd->size;
  317. MAP_INFO(map, "reducing map size to %ldKiB\n",
  318. (unsigned long)map->size >> 10);
  319. err = adm5120_flash_initres(info);
  320. if (err)
  321. goto err_out;
  322. }
  323. MAP_INFO(map, "found at 0x%lX, size:%ldKiB, width:%d bits\n",
  324. (unsigned long)map->phys, (unsigned long)info->mtd->size >> 10,
  325. map->bankwidth*8);
  326. info->mtd->owner = THIS_MODULE;
  327. err = mtd_device_parse_register(info->mtd, parse_types, 0,
  328. pdata->parts, pdata->nr_parts);
  329. if (err)
  330. goto err_out;
  331. return 0;
  332. err_out:
  333. adm5120_flash_remove(dev);
  334. return err;
  335. }
  336. #ifdef CONFIG_PM
  337. static int adm5120_flash_suspend(struct platform_device *dev,
  338. pm_message_t state)
  339. {
  340. struct adm5120_flash_info *info = platform_get_drvdata(dev);
  341. int ret = 0;
  342. if (info)
  343. ret = info->mtd->suspend(info->mtd);
  344. return ret;
  345. }
  346. static int adm5120_flash_resume(struct platform_device *dev)
  347. {
  348. struct adm5120_flash_info *info = platform_get_drvdata(dev);
  349. if (info)
  350. info->mtd->resume(info->mtd);
  351. return 0;
  352. }
  353. static void adm5120_flash_shutdown(struct platform_device *dev)
  354. {
  355. struct adm5120_flash_info *info = platform_get_drvdata(dev);
  356. if (info && info->mtd->suspend(info->mtd) == 0)
  357. info->mtd->resume(info->mtd);
  358. }
  359. #endif
  360. static struct platform_driver adm5120_flash_driver = {
  361. .probe = adm5120_flash_probe,
  362. .remove = adm5120_flash_remove,
  363. #ifdef CONFIG_PM
  364. .suspend = adm5120_flash_suspend,
  365. .resume = adm5120_flash_resume,
  366. .shutdown = adm5120_flash_shutdown,
  367. #endif
  368. .driver = {
  369. .name = DRV_NAME,
  370. },
  371. };
  372. static int __init adm5120_flash_init(void)
  373. {
  374. int err;
  375. err = platform_driver_register(&adm5120_flash_driver);
  376. return err;
  377. }
  378. static void __exit adm5120_flash_exit(void)
  379. {
  380. platform_driver_unregister(&adm5120_flash_driver);
  381. }
  382. module_init(adm5120_flash_init);
  383. module_exit(adm5120_flash_exit);
  384. MODULE_LICENSE("GPL v2");
  385. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  386. MODULE_DESCRIPTION(DRV_DESC);