spear_smi.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*
  2. * SMI (Serial Memory Controller) device driver for Serial NOR Flash on
  3. * SPEAr platform
  4. * The serial nor interface is largely based on drivers/mtd/m25p80.c,
  5. * however the SPI interface has been replaced by SMI.
  6. *
  7. * Copyright © 2010 STMicroelectronics.
  8. * Ashish Priyadarshi
  9. * Shiraz Hashim <shiraz.hashim@st.com>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/ioport.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/param.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/mtd/mtd.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/mtd/spear_smi.h>
  31. #include <linux/mutex.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #include <linux/wait.h>
  35. /* max possible slots for serial-nor flash chip in the SMI controller */
  36. #define MAX_NUM_FLASH_CHIP 4
  37. /* SMI clock rate */
  38. #define SMI_MAX_CLOCK_FREQ 50000000 /* 50 MHz */
  39. /* MAX time out to safely come out of a erase or write busy conditions */
  40. #define SMI_PROBE_TIMEOUT (HZ / 10)
  41. #define SMI_MAX_TIME_OUT (3 * HZ)
  42. /* timeout for command completion */
  43. #define SMI_CMD_TIMEOUT (HZ / 10)
  44. /* registers of smi */
  45. #define SMI_CR1 0x0 /* SMI control register 1 */
  46. #define SMI_CR2 0x4 /* SMI control register 2 */
  47. #define SMI_SR 0x8 /* SMI status register */
  48. #define SMI_TR 0xC /* SMI transmit register */
  49. #define SMI_RR 0x10 /* SMI receive register */
  50. /* defines for control_reg 1 */
  51. #define BANK_EN (0xF << 0) /* enables all banks */
  52. #define DSEL_TIME (0x6 << 4) /* Deselect time 6 + 1 SMI_CK periods */
  53. #define SW_MODE (0x1 << 28) /* enables SW Mode */
  54. #define WB_MODE (0x1 << 29) /* Write Burst Mode */
  55. #define FAST_MODE (0x1 << 15) /* Fast Mode */
  56. #define HOLD1 (0x1 << 16) /* Clock Hold period selection */
  57. /* defines for control_reg 2 */
  58. #define SEND (0x1 << 7) /* Send data */
  59. #define TFIE (0x1 << 8) /* Transmission Flag Interrupt Enable */
  60. #define WCIE (0x1 << 9) /* Write Complete Interrupt Enable */
  61. #define RD_STATUS_REG (0x1 << 10) /* reads status reg */
  62. #define WE (0x1 << 11) /* Write Enable */
  63. #define TX_LEN_SHIFT 0
  64. #define RX_LEN_SHIFT 4
  65. #define BANK_SHIFT 12
  66. /* defines for status register */
  67. #define SR_WIP 0x1 /* Write in progress */
  68. #define SR_WEL 0x2 /* Write enable latch */
  69. #define SR_BP0 0x4 /* Block protect 0 */
  70. #define SR_BP1 0x8 /* Block protect 1 */
  71. #define SR_BP2 0x10 /* Block protect 2 */
  72. #define SR_SRWD 0x80 /* SR write protect */
  73. #define TFF 0x100 /* Transfer Finished Flag */
  74. #define WCF 0x200 /* Transfer Finished Flag */
  75. #define ERF1 0x400 /* Forbidden Write Request */
  76. #define ERF2 0x800 /* Forbidden Access */
  77. #define WM_SHIFT 12
  78. /* flash opcodes */
  79. #define OPCODE_RDID 0x9f /* Read JEDEC ID */
  80. /* Flash Device Ids maintenance section */
  81. /* data structure to maintain flash ids from different vendors */
  82. struct flash_device {
  83. char *name;
  84. u8 erase_cmd;
  85. u32 device_id;
  86. u32 pagesize;
  87. unsigned long sectorsize;
  88. unsigned long size_in_bytes;
  89. };
  90. #define FLASH_ID(n, es, id, psize, ssize, size) \
  91. { \
  92. .name = n, \
  93. .erase_cmd = es, \
  94. .device_id = id, \
  95. .pagesize = psize, \
  96. .sectorsize = ssize, \
  97. .size_in_bytes = size \
  98. }
  99. static struct flash_device flash_devices[] = {
  100. FLASH_ID("st m25p16" , 0xd8, 0x00152020, 0x100, 0x10000, 0x200000),
  101. FLASH_ID("st m25p32" , 0xd8, 0x00162020, 0x100, 0x10000, 0x400000),
  102. FLASH_ID("st m25p64" , 0xd8, 0x00172020, 0x100, 0x10000, 0x800000),
  103. FLASH_ID("st m25p128" , 0xd8, 0x00182020, 0x100, 0x40000, 0x1000000),
  104. FLASH_ID("st m25p05" , 0xd8, 0x00102020, 0x80 , 0x8000 , 0x10000),
  105. FLASH_ID("st m25p10" , 0xd8, 0x00112020, 0x80 , 0x8000 , 0x20000),
  106. FLASH_ID("st m25p20" , 0xd8, 0x00122020, 0x100, 0x10000, 0x40000),
  107. FLASH_ID("st m25p40" , 0xd8, 0x00132020, 0x100, 0x10000, 0x80000),
  108. FLASH_ID("st m25p80" , 0xd8, 0x00142020, 0x100, 0x10000, 0x100000),
  109. FLASH_ID("st m45pe10" , 0xd8, 0x00114020, 0x100, 0x10000, 0x20000),
  110. FLASH_ID("st m45pe20" , 0xd8, 0x00124020, 0x100, 0x10000, 0x40000),
  111. FLASH_ID("st m45pe40" , 0xd8, 0x00134020, 0x100, 0x10000, 0x80000),
  112. FLASH_ID("st m45pe80" , 0xd8, 0x00144020, 0x100, 0x10000, 0x100000),
  113. FLASH_ID("sp s25fl004" , 0xd8, 0x00120201, 0x100, 0x10000, 0x80000),
  114. FLASH_ID("sp s25fl008" , 0xd8, 0x00130201, 0x100, 0x10000, 0x100000),
  115. FLASH_ID("sp s25fl016" , 0xd8, 0x00140201, 0x100, 0x10000, 0x200000),
  116. FLASH_ID("sp s25fl032" , 0xd8, 0x00150201, 0x100, 0x10000, 0x400000),
  117. FLASH_ID("sp s25fl064" , 0xd8, 0x00160201, 0x100, 0x10000, 0x800000),
  118. FLASH_ID("atmel 25f512" , 0x52, 0x0065001F, 0x80 , 0x8000 , 0x10000),
  119. FLASH_ID("atmel 25f1024" , 0x52, 0x0060001F, 0x100, 0x8000 , 0x20000),
  120. FLASH_ID("atmel 25f2048" , 0x52, 0x0063001F, 0x100, 0x10000, 0x40000),
  121. FLASH_ID("atmel 25f4096" , 0x52, 0x0064001F, 0x100, 0x10000, 0x80000),
  122. FLASH_ID("atmel 25fs040" , 0xd7, 0x0004661F, 0x100, 0x10000, 0x80000),
  123. FLASH_ID("mac 25l512" , 0xd8, 0x001020C2, 0x010, 0x10000, 0x10000),
  124. FLASH_ID("mac 25l1005" , 0xd8, 0x001120C2, 0x010, 0x10000, 0x20000),
  125. FLASH_ID("mac 25l2005" , 0xd8, 0x001220C2, 0x010, 0x10000, 0x40000),
  126. FLASH_ID("mac 25l4005" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
  127. FLASH_ID("mac 25l4005a" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
  128. FLASH_ID("mac 25l8005" , 0xd8, 0x001420C2, 0x010, 0x10000, 0x100000),
  129. FLASH_ID("mac 25l1605" , 0xd8, 0x001520C2, 0x100, 0x10000, 0x200000),
  130. FLASH_ID("mac 25l1605a" , 0xd8, 0x001520C2, 0x010, 0x10000, 0x200000),
  131. FLASH_ID("mac 25l3205" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
  132. FLASH_ID("mac 25l3205a" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
  133. FLASH_ID("mac 25l6405" , 0xd8, 0x001720C2, 0x100, 0x10000, 0x800000),
  134. };
  135. /* These partitions would be used if platform doesn't pass one */
  136. static struct mtd_partition part_info_8M[] = {
  137. DEFINE_PARTS("Xloader", 0x00, 0x10000),
  138. DEFINE_PARTS("UBoot", MTDPART_OFS_APPEND, 0x40000),
  139. DEFINE_PARTS("Kernel", MTDPART_OFS_APPEND, 0x2C0000),
  140. DEFINE_PARTS("Root File System", MTDPART_OFS_APPEND, MTDPART_SIZ_FULL),
  141. };
  142. static struct mtd_partition part_info_16M[] = {
  143. DEFINE_PARTS("Xloader", 0x00, 0x40000),
  144. DEFINE_PARTS("UBoot", MTDPART_OFS_APPEND, 0x100000),
  145. DEFINE_PARTS("Kernel", MTDPART_OFS_APPEND, 0x300000),
  146. DEFINE_PARTS("Root File System", MTDPART_OFS_APPEND, MTDPART_SIZ_FULL),
  147. };
  148. /* Define spear specific structures */
  149. struct spear_snor_flash;
  150. /**
  151. * struct spear_smi - Structure for SMI Device
  152. *
  153. * @clk: functional clock
  154. * @status: current status register of SMI.
  155. * @clk_rate: functional clock rate of SMI (default: SMI_MAX_CLOCK_FREQ)
  156. * @lock: lock to prevent parallel access of SMI.
  157. * @io_base: base address for registers of SMI.
  158. * @pdev: platform device
  159. * @cmd_complete: queue to wait for command completion of NOR-flash.
  160. * @num_flashes: number of flashes actually present on board.
  161. * @flash: separate structure for each Serial NOR-flash attached to SMI.
  162. */
  163. struct spear_smi {
  164. struct clk *clk;
  165. u32 status;
  166. unsigned long clk_rate;
  167. struct mutex lock;
  168. void __iomem *io_base;
  169. struct platform_device *pdev;
  170. wait_queue_head_t cmd_complete;
  171. u32 num_flashes;
  172. struct spear_snor_flash *flash[MAX_NUM_FLASH_CHIP];
  173. };
  174. /**
  175. * struct spear_snor_flash - Structure for Serial NOR Flash
  176. *
  177. * @bank: Bank number(0, 1, 2, 3) for each NOR-flash.
  178. * @dev_id: Device ID of NOR-flash.
  179. * @lock: lock to manage flash read, write and erase operations
  180. * @mtd: MTD info for each NOR-flash.
  181. * @num_parts: Total number of partition in each bank of NOR-flash.
  182. * @parts: Partition info for each bank of NOR-flash.
  183. * @page_size: Page size of NOR-flash.
  184. * @base_addr: Base address of NOR-flash.
  185. * @erase_cmd: erase command may vary on different flash types
  186. * @fast_mode: flash supports read in fast mode
  187. */
  188. struct spear_snor_flash {
  189. u32 bank;
  190. u32 dev_id;
  191. struct mutex lock;
  192. struct mtd_info mtd;
  193. u32 num_parts;
  194. struct mtd_partition *parts;
  195. u32 page_size;
  196. void __iomem *base_addr;
  197. u8 erase_cmd;
  198. u8 fast_mode;
  199. };
  200. static inline struct spear_snor_flash *get_flash_data(struct mtd_info *mtd)
  201. {
  202. return container_of(mtd, struct spear_snor_flash, mtd);
  203. }
  204. /**
  205. * spear_smi_read_sr - Read status register of flash through SMI
  206. * @dev: structure of SMI information.
  207. * @bank: bank to which flash is connected
  208. *
  209. * This routine will return the status register of the flash chip present at the
  210. * given bank.
  211. */
  212. static int spear_smi_read_sr(struct spear_smi *dev, u32 bank)
  213. {
  214. int ret;
  215. u32 ctrlreg1;
  216. mutex_lock(&dev->lock);
  217. dev->status = 0; /* Will be set in interrupt handler */
  218. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  219. /* program smi in hw mode */
  220. writel(ctrlreg1 & ~(SW_MODE | WB_MODE), dev->io_base + SMI_CR1);
  221. /* performing a rsr instruction in hw mode */
  222. writel((bank << BANK_SHIFT) | RD_STATUS_REG | TFIE,
  223. dev->io_base + SMI_CR2);
  224. /* wait for tff */
  225. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  226. dev->status & TFF, SMI_CMD_TIMEOUT);
  227. /* copy dev->status (lower 16 bits) in order to release lock */
  228. if (ret > 0)
  229. ret = dev->status & 0xffff;
  230. else
  231. ret = -EIO;
  232. /* restore the ctrl regs state */
  233. writel(ctrlreg1, dev->io_base + SMI_CR1);
  234. writel(0, dev->io_base + SMI_CR2);
  235. mutex_unlock(&dev->lock);
  236. return ret;
  237. }
  238. /**
  239. * spear_smi_wait_till_ready - wait till flash is ready
  240. * @dev: structure of SMI information.
  241. * @bank: flash corresponding to this bank
  242. * @timeout: timeout for busy wait condition
  243. *
  244. * This routine checks for WIP (write in progress) bit in Status register
  245. * If successful the routine returns 0 else -EBUSY
  246. */
  247. static int spear_smi_wait_till_ready(struct spear_smi *dev, u32 bank,
  248. unsigned long timeout)
  249. {
  250. unsigned long finish;
  251. int status;
  252. finish = jiffies + timeout;
  253. do {
  254. status = spear_smi_read_sr(dev, bank);
  255. if (status < 0)
  256. continue; /* try till timeout */
  257. else if (!(status & SR_WIP))
  258. return 0;
  259. cond_resched();
  260. } while (!time_after_eq(jiffies, finish));
  261. dev_err(&dev->pdev->dev, "smi controller is busy, timeout\n");
  262. return status;
  263. }
  264. /**
  265. * spear_smi_int_handler - SMI Interrupt Handler.
  266. * @irq: irq number
  267. * @dev_id: structure of SMI device, embedded in dev_id.
  268. *
  269. * The handler clears all interrupt conditions and records the status in
  270. * dev->status which is used by the driver later.
  271. */
  272. static irqreturn_t spear_smi_int_handler(int irq, void *dev_id)
  273. {
  274. u32 status = 0;
  275. struct spear_smi *dev = dev_id;
  276. status = readl(dev->io_base + SMI_SR);
  277. if (unlikely(!status))
  278. return IRQ_NONE;
  279. /* clear all interrupt conditions */
  280. writel(0, dev->io_base + SMI_SR);
  281. /* copy the status register in dev->status */
  282. dev->status |= status;
  283. /* send the completion */
  284. wake_up_interruptible(&dev->cmd_complete);
  285. return IRQ_HANDLED;
  286. }
  287. /**
  288. * spear_smi_hw_init - initializes the smi controller.
  289. * @dev: structure of smi device
  290. *
  291. * this routine initializes the smi controller wit the default values
  292. */
  293. static void spear_smi_hw_init(struct spear_smi *dev)
  294. {
  295. unsigned long rate = 0;
  296. u32 prescale = 0;
  297. u32 val;
  298. rate = clk_get_rate(dev->clk);
  299. /* functional clock of smi */
  300. prescale = DIV_ROUND_UP(rate, dev->clk_rate);
  301. /*
  302. * setting the standard values, fast mode, prescaler for
  303. * SMI_MAX_CLOCK_FREQ (50MHz) operation and bank enable
  304. */
  305. val = HOLD1 | BANK_EN | DSEL_TIME | (prescale << 8);
  306. mutex_lock(&dev->lock);
  307. writel(val, dev->io_base + SMI_CR1);
  308. mutex_unlock(&dev->lock);
  309. }
  310. /**
  311. * get_flash_index - match chip id from a flash list.
  312. * @flash_id: a valid nor flash chip id obtained from board.
  313. *
  314. * try to validate the chip id by matching from a list, if not found then simply
  315. * returns negative. In case of success returns index in to the flash devices
  316. * array.
  317. */
  318. static int get_flash_index(u32 flash_id)
  319. {
  320. int index;
  321. /* Matches chip-id to entire list of 'serial-nor flash' ids */
  322. for (index = 0; index < ARRAY_SIZE(flash_devices); index++) {
  323. if (flash_devices[index].device_id == flash_id)
  324. return index;
  325. }
  326. /* Memory chip is not listed and not supported */
  327. return -ENODEV;
  328. }
  329. /**
  330. * spear_smi_write_enable - Enable the flash to do write operation
  331. * @dev: structure of SMI device
  332. * @bank: enable write for flash connected to this bank
  333. *
  334. * Set write enable latch with Write Enable command.
  335. * Returns 0 on success.
  336. */
  337. static int spear_smi_write_enable(struct spear_smi *dev, u32 bank)
  338. {
  339. int ret;
  340. u32 ctrlreg1;
  341. mutex_lock(&dev->lock);
  342. dev->status = 0; /* Will be set in interrupt handler */
  343. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  344. /* program smi in h/w mode */
  345. writel(ctrlreg1 & ~SW_MODE, dev->io_base + SMI_CR1);
  346. /* give the flash, write enable command */
  347. writel((bank << BANK_SHIFT) | WE | TFIE, dev->io_base + SMI_CR2);
  348. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  349. dev->status & TFF, SMI_CMD_TIMEOUT);
  350. /* restore the ctrl regs state */
  351. writel(ctrlreg1, dev->io_base + SMI_CR1);
  352. writel(0, dev->io_base + SMI_CR2);
  353. if (ret <= 0) {
  354. ret = -EIO;
  355. dev_err(&dev->pdev->dev,
  356. "smi controller failed on write enable\n");
  357. } else {
  358. /* check whether write mode status is set for required bank */
  359. if (dev->status & (1 << (bank + WM_SHIFT)))
  360. ret = 0;
  361. else {
  362. dev_err(&dev->pdev->dev, "couldn't enable write\n");
  363. ret = -EIO;
  364. }
  365. }
  366. mutex_unlock(&dev->lock);
  367. return ret;
  368. }
  369. static inline u32
  370. get_sector_erase_cmd(struct spear_snor_flash *flash, u32 offset)
  371. {
  372. u32 cmd;
  373. u8 *x = (u8 *)&cmd;
  374. x[0] = flash->erase_cmd;
  375. x[1] = offset >> 16;
  376. x[2] = offset >> 8;
  377. x[3] = offset;
  378. return cmd;
  379. }
  380. /**
  381. * spear_smi_erase_sector - erase one sector of flash
  382. * @dev: structure of SMI information
  383. * @command: erase command to be send
  384. * @bank: bank to which this command needs to be send
  385. * @bytes: size of command
  386. *
  387. * Erase one sector of flash memory at offset ``offset'' which is any
  388. * address within the sector which should be erased.
  389. * Returns 0 if successful, non-zero otherwise.
  390. */
  391. static int spear_smi_erase_sector(struct spear_smi *dev,
  392. u32 bank, u32 command, u32 bytes)
  393. {
  394. u32 ctrlreg1 = 0;
  395. int ret;
  396. ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
  397. if (ret)
  398. return ret;
  399. ret = spear_smi_write_enable(dev, bank);
  400. if (ret)
  401. return ret;
  402. mutex_lock(&dev->lock);
  403. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  404. writel((ctrlreg1 | SW_MODE) & ~WB_MODE, dev->io_base + SMI_CR1);
  405. /* send command in sw mode */
  406. writel(command, dev->io_base + SMI_TR);
  407. writel((bank << BANK_SHIFT) | SEND | TFIE | (bytes << TX_LEN_SHIFT),
  408. dev->io_base + SMI_CR2);
  409. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  410. dev->status & TFF, SMI_CMD_TIMEOUT);
  411. if (ret <= 0) {
  412. ret = -EIO;
  413. dev_err(&dev->pdev->dev, "sector erase failed\n");
  414. } else
  415. ret = 0; /* success */
  416. /* restore ctrl regs */
  417. writel(ctrlreg1, dev->io_base + SMI_CR1);
  418. writel(0, dev->io_base + SMI_CR2);
  419. mutex_unlock(&dev->lock);
  420. return ret;
  421. }
  422. /**
  423. * spear_mtd_erase - perform flash erase operation as requested by user
  424. * @mtd: Provides the memory characteristics
  425. * @e_info: Provides the erase information
  426. *
  427. * Erase an address range on the flash chip. The address range may extend
  428. * one or more erase sectors. Return an error is there is a problem erasing.
  429. */
  430. static int spear_mtd_erase(struct mtd_info *mtd, struct erase_info *e_info)
  431. {
  432. struct spear_snor_flash *flash = get_flash_data(mtd);
  433. struct spear_smi *dev = mtd->priv;
  434. u32 addr, command, bank;
  435. int len, ret;
  436. if (!flash || !dev)
  437. return -ENODEV;
  438. bank = flash->bank;
  439. if (bank > dev->num_flashes - 1) {
  440. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  441. return -EINVAL;
  442. }
  443. addr = e_info->addr;
  444. len = e_info->len;
  445. mutex_lock(&flash->lock);
  446. /* now erase sectors in loop */
  447. while (len) {
  448. command = get_sector_erase_cmd(flash, addr);
  449. /* preparing the command for flash */
  450. ret = spear_smi_erase_sector(dev, bank, command, 4);
  451. if (ret) {
  452. e_info->state = MTD_ERASE_FAILED;
  453. mutex_unlock(&flash->lock);
  454. return ret;
  455. }
  456. addr += mtd->erasesize;
  457. len -= mtd->erasesize;
  458. }
  459. mutex_unlock(&flash->lock);
  460. e_info->state = MTD_ERASE_DONE;
  461. mtd_erase_callback(e_info);
  462. return 0;
  463. }
  464. /**
  465. * spear_mtd_read - performs flash read operation as requested by the user
  466. * @mtd: MTD information of the memory bank
  467. * @from: Address from which to start read
  468. * @len: Number of bytes to be read
  469. * @retlen: Fills the Number of bytes actually read
  470. * @buf: Fills this after reading
  471. *
  472. * Read an address range from the flash chip. The address range
  473. * may be any size provided it is within the physical boundaries.
  474. * Returns 0 on success, non zero otherwise
  475. */
  476. static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  477. size_t *retlen, u8 *buf)
  478. {
  479. struct spear_snor_flash *flash = get_flash_data(mtd);
  480. struct spear_smi *dev = mtd->priv;
  481. void *src;
  482. u32 ctrlreg1, val;
  483. int ret;
  484. if (!flash || !dev)
  485. return -ENODEV;
  486. if (flash->bank > dev->num_flashes - 1) {
  487. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  488. return -EINVAL;
  489. }
  490. /* select address as per bank number */
  491. src = flash->base_addr + from;
  492. mutex_lock(&flash->lock);
  493. /* wait till previous write/erase is done. */
  494. ret = spear_smi_wait_till_ready(dev, flash->bank, SMI_MAX_TIME_OUT);
  495. if (ret) {
  496. mutex_unlock(&flash->lock);
  497. return ret;
  498. }
  499. mutex_lock(&dev->lock);
  500. /* put smi in hw mode not wbt mode */
  501. ctrlreg1 = val = readl(dev->io_base + SMI_CR1);
  502. val &= ~(SW_MODE | WB_MODE);
  503. if (flash->fast_mode)
  504. val |= FAST_MODE;
  505. writel(val, dev->io_base + SMI_CR1);
  506. memcpy_fromio(buf, (u8 *)src, len);
  507. /* restore ctrl reg1 */
  508. writel(ctrlreg1, dev->io_base + SMI_CR1);
  509. mutex_unlock(&dev->lock);
  510. *retlen = len;
  511. mutex_unlock(&flash->lock);
  512. return 0;
  513. }
  514. static inline int spear_smi_cpy_toio(struct spear_smi *dev, u32 bank,
  515. void *dest, const void *src, size_t len)
  516. {
  517. int ret;
  518. u32 ctrlreg1;
  519. /* wait until finished previous write command. */
  520. ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
  521. if (ret)
  522. return ret;
  523. /* put smi in write enable */
  524. ret = spear_smi_write_enable(dev, bank);
  525. if (ret)
  526. return ret;
  527. /* put smi in hw, write burst mode */
  528. mutex_lock(&dev->lock);
  529. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  530. writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1);
  531. memcpy_toio(dest, src, len);
  532. writel(ctrlreg1, dev->io_base + SMI_CR1);
  533. mutex_unlock(&dev->lock);
  534. return 0;
  535. }
  536. /**
  537. * spear_mtd_write - performs write operation as requested by the user.
  538. * @mtd: MTD information of the memory bank.
  539. * @to: Address to write.
  540. * @len: Number of bytes to be written.
  541. * @retlen: Number of bytes actually wrote.
  542. * @buf: Buffer from which the data to be taken.
  543. *
  544. * Write an address range to the flash chip. Data must be written in
  545. * flash_page_size chunks. The address range may be any size provided
  546. * it is within the physical boundaries.
  547. * Returns 0 on success, non zero otherwise
  548. */
  549. static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  550. size_t *retlen, const u8 *buf)
  551. {
  552. struct spear_snor_flash *flash = get_flash_data(mtd);
  553. struct spear_smi *dev = mtd->priv;
  554. void *dest;
  555. u32 page_offset, page_size;
  556. int ret;
  557. if (!flash || !dev)
  558. return -ENODEV;
  559. if (flash->bank > dev->num_flashes - 1) {
  560. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  561. return -EINVAL;
  562. }
  563. /* select address as per bank number */
  564. dest = flash->base_addr + to;
  565. mutex_lock(&flash->lock);
  566. page_offset = (u32)to % flash->page_size;
  567. /* do if all the bytes fit onto one page */
  568. if (page_offset + len <= flash->page_size) {
  569. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, len);
  570. if (!ret)
  571. *retlen += len;
  572. } else {
  573. u32 i;
  574. /* the size of data remaining on the first page */
  575. page_size = flash->page_size - page_offset;
  576. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf,
  577. page_size);
  578. if (ret)
  579. goto err_write;
  580. else
  581. *retlen += page_size;
  582. /* write everything in pagesize chunks */
  583. for (i = page_size; i < len; i += page_size) {
  584. page_size = len - i;
  585. if (page_size > flash->page_size)
  586. page_size = flash->page_size;
  587. ret = spear_smi_cpy_toio(dev, flash->bank, dest + i,
  588. buf + i, page_size);
  589. if (ret)
  590. break;
  591. else
  592. *retlen += page_size;
  593. }
  594. }
  595. err_write:
  596. mutex_unlock(&flash->lock);
  597. return ret;
  598. }
  599. /**
  600. * spear_smi_probe_flash - Detects the NOR Flash chip.
  601. * @dev: structure of SMI information.
  602. * @bank: bank on which flash must be probed
  603. *
  604. * This routine will check whether there exists a flash chip on a given memory
  605. * bank ID.
  606. * Return index of the probed flash in flash devices structure
  607. */
  608. static int spear_smi_probe_flash(struct spear_smi *dev, u32 bank)
  609. {
  610. int ret;
  611. u32 val = 0;
  612. ret = spear_smi_wait_till_ready(dev, bank, SMI_PROBE_TIMEOUT);
  613. if (ret)
  614. return ret;
  615. mutex_lock(&dev->lock);
  616. dev->status = 0; /* Will be set in interrupt handler */
  617. /* put smi in sw mode */
  618. val = readl(dev->io_base + SMI_CR1);
  619. writel(val | SW_MODE, dev->io_base + SMI_CR1);
  620. /* send readid command in sw mode */
  621. writel(OPCODE_RDID, dev->io_base + SMI_TR);
  622. val = (bank << BANK_SHIFT) | SEND | (1 << TX_LEN_SHIFT) |
  623. (3 << RX_LEN_SHIFT) | TFIE;
  624. writel(val, dev->io_base + SMI_CR2);
  625. /* wait for TFF */
  626. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  627. dev->status & TFF, SMI_CMD_TIMEOUT);
  628. if (ret <= 0) {
  629. ret = -ENODEV;
  630. goto err_probe;
  631. }
  632. /* get memory chip id */
  633. val = readl(dev->io_base + SMI_RR);
  634. val &= 0x00ffffff;
  635. ret = get_flash_index(val);
  636. err_probe:
  637. /* clear sw mode */
  638. val = readl(dev->io_base + SMI_CR1);
  639. writel(val & ~SW_MODE, dev->io_base + SMI_CR1);
  640. mutex_unlock(&dev->lock);
  641. return ret;
  642. }
  643. static int spear_smi_setup_banks(struct platform_device *pdev, u32 bank)
  644. {
  645. struct spear_smi *dev = platform_get_drvdata(pdev);
  646. struct spear_smi_flash_info *flash_info;
  647. struct spear_smi_plat_data *pdata;
  648. struct spear_snor_flash *flash;
  649. struct mtd_partition *parts;
  650. int count;
  651. int flash_index;
  652. int ret = 0;
  653. pdata = dev_get_platdata(&pdev->dev);
  654. if (bank > pdata->num_flashes - 1)
  655. return -EINVAL;
  656. flash_info = &pdata->board_flash_info[bank];
  657. if (!flash_info)
  658. return -ENODEV;
  659. flash = kzalloc(sizeof(*flash), GFP_ATOMIC);
  660. if (!flash)
  661. return -ENOMEM;
  662. flash->bank = bank;
  663. flash->fast_mode = flash_info->fast_mode ? 1 : 0;
  664. mutex_init(&flash->lock);
  665. /* verify whether nor flash is really present on board */
  666. flash_index = spear_smi_probe_flash(dev, bank);
  667. if (flash_index < 0) {
  668. dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank);
  669. ret = flash_index;
  670. goto err_probe;
  671. }
  672. /* map the memory for nor flash chip */
  673. flash->base_addr = ioremap(flash_info->mem_base, flash_info->size);
  674. if (!flash->base_addr) {
  675. ret = -EIO;
  676. goto err_probe;
  677. }
  678. dev->flash[bank] = flash;
  679. flash->mtd.priv = dev;
  680. if (flash_info->name)
  681. flash->mtd.name = flash_info->name;
  682. else
  683. flash->mtd.name = flash_devices[flash_index].name;
  684. flash->mtd.type = MTD_NORFLASH;
  685. flash->mtd.writesize = 1;
  686. flash->mtd.flags = MTD_CAP_NORFLASH;
  687. flash->mtd.size = flash_info->size;
  688. flash->mtd.erasesize = flash_devices[flash_index].sectorsize;
  689. flash->page_size = flash_devices[flash_index].pagesize;
  690. flash->mtd.writebufsize = flash->page_size;
  691. flash->erase_cmd = flash_devices[flash_index].erase_cmd;
  692. flash->mtd._erase = spear_mtd_erase;
  693. flash->mtd._read = spear_mtd_read;
  694. flash->mtd._write = spear_mtd_write;
  695. flash->dev_id = flash_devices[flash_index].device_id;
  696. dev_info(&dev->pdev->dev, "mtd .name=%s .size=%llx(%lluM)\n",
  697. flash->mtd.name, flash->mtd.size,
  698. flash->mtd.size / (1024 * 1024));
  699. dev_info(&dev->pdev->dev, ".erasesize = 0x%x(%uK)\n",
  700. flash->mtd.erasesize, flash->mtd.erasesize / 1024);
  701. if (flash_info->partitions) {
  702. parts = flash_info->partitions;
  703. count = flash_info->nr_partitions;
  704. } else {
  705. /* choose from default ones */
  706. switch (flash->mtd.size) {
  707. case 0x800000:/* 8MB */
  708. parts = part_info_8M;
  709. count = ARRAY_SIZE(part_info_8M);
  710. break;
  711. case 0x1000000:/* 16MB */
  712. parts = part_info_16M;
  713. count = ARRAY_SIZE(part_info_16M);
  714. break;
  715. default:
  716. dev_err(&pdev->dev, "undefined partition\n");
  717. ret = ENODEV;
  718. goto err_map;
  719. }
  720. }
  721. ret = mtd_device_parse_register(&flash->mtd, NULL, NULL, parts, count);
  722. if (ret)
  723. dev_err(&dev->pdev->dev, "Err MTD partition=%d\n", ret);
  724. return ret;
  725. err_map:
  726. iounmap(flash->base_addr);
  727. err_probe:
  728. kfree(flash);
  729. return ret;
  730. }
  731. /**
  732. * spear_smi_probe - Entry routine
  733. * @pdev: platform device structure
  734. *
  735. * This is the first routine which gets invoked during booting and does all
  736. * initialization/allocation work. The routine looks for available memory banks,
  737. * and do proper init for any found one.
  738. * Returns 0 on success, non zero otherwise
  739. */
  740. static int __devinit spear_smi_probe(struct platform_device *pdev)
  741. {
  742. struct spear_smi_plat_data *pdata;
  743. struct spear_smi *dev;
  744. struct resource *smi_base;
  745. int irq, ret = 0;
  746. int i;
  747. pdata = dev_get_platdata(&pdev->dev);
  748. if (pdata < 0) {
  749. ret = -ENODEV;
  750. dev_err(&pdev->dev, "no platform data\n");
  751. goto err;
  752. }
  753. smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  754. if (!smi_base) {
  755. ret = -ENODEV;
  756. dev_err(&pdev->dev, "invalid smi base address\n");
  757. goto err;
  758. }
  759. irq = platform_get_irq(pdev, 0);
  760. if (irq < 0) {
  761. ret = -ENODEV;
  762. dev_err(&pdev->dev, "invalid smi irq\n");
  763. goto err;
  764. }
  765. dev = kzalloc(sizeof(*dev), GFP_ATOMIC);
  766. if (!dev) {
  767. ret = -ENOMEM;
  768. dev_err(&pdev->dev, "mem alloc fail\n");
  769. goto err;
  770. }
  771. smi_base = request_mem_region(smi_base->start, resource_size(smi_base),
  772. pdev->name);
  773. if (!smi_base) {
  774. ret = -EBUSY;
  775. dev_err(&pdev->dev, "request mem region fail\n");
  776. goto err_mem;
  777. }
  778. dev->io_base = ioremap(smi_base->start, resource_size(smi_base));
  779. if (!dev->io_base) {
  780. ret = -EIO;
  781. dev_err(&pdev->dev, "ioremap fail\n");
  782. goto err_ioremap;
  783. }
  784. dev->pdev = pdev;
  785. dev->clk_rate = pdata->clk_rate;
  786. if (dev->clk_rate < 0 || dev->clk_rate > SMI_MAX_CLOCK_FREQ)
  787. dev->clk_rate = SMI_MAX_CLOCK_FREQ;
  788. dev->num_flashes = pdata->num_flashes;
  789. if (dev->num_flashes > MAX_NUM_FLASH_CHIP) {
  790. dev_err(&pdev->dev, "exceeding max number of flashes\n");
  791. dev->num_flashes = MAX_NUM_FLASH_CHIP;
  792. }
  793. dev->clk = clk_get(&pdev->dev, NULL);
  794. if (IS_ERR(dev->clk)) {
  795. ret = PTR_ERR(dev->clk);
  796. goto err_clk;
  797. }
  798. ret = clk_enable(dev->clk);
  799. if (ret)
  800. goto err_clk_enable;
  801. ret = request_irq(irq, spear_smi_int_handler, 0, pdev->name, dev);
  802. if (ret) {
  803. dev_err(&dev->pdev->dev, "SMI IRQ allocation failed\n");
  804. goto err_irq;
  805. }
  806. mutex_init(&dev->lock);
  807. init_waitqueue_head(&dev->cmd_complete);
  808. spear_smi_hw_init(dev);
  809. platform_set_drvdata(pdev, dev);
  810. /* loop for each serial nor-flash which is connected to smi */
  811. for (i = 0; i < dev->num_flashes; i++) {
  812. ret = spear_smi_setup_banks(pdev, i);
  813. if (ret) {
  814. dev_err(&dev->pdev->dev, "bank setup failed\n");
  815. goto err_bank_setup;
  816. }
  817. }
  818. return 0;
  819. err_bank_setup:
  820. free_irq(irq, dev);
  821. platform_set_drvdata(pdev, NULL);
  822. err_irq:
  823. clk_disable(dev->clk);
  824. err_clk_enable:
  825. clk_put(dev->clk);
  826. err_clk:
  827. iounmap(dev->io_base);
  828. err_ioremap:
  829. release_mem_region(smi_base->start, resource_size(smi_base));
  830. err_mem:
  831. kfree(dev);
  832. err:
  833. return ret;
  834. }
  835. /**
  836. * spear_smi_remove - Exit routine
  837. * @pdev: platform device structure
  838. *
  839. * free all allocations and delete the partitions.
  840. */
  841. static int __devexit spear_smi_remove(struct platform_device *pdev)
  842. {
  843. struct spear_smi *dev;
  844. struct spear_snor_flash *flash;
  845. struct resource *smi_base;
  846. int ret;
  847. int i, irq;
  848. dev = platform_get_drvdata(pdev);
  849. if (!dev) {
  850. dev_err(&pdev->dev, "dev is null\n");
  851. return -ENODEV;
  852. }
  853. /* clean up for all nor flash */
  854. for (i = 0; i < dev->num_flashes; i++) {
  855. flash = dev->flash[i];
  856. if (!flash)
  857. continue;
  858. /* clean up mtd stuff */
  859. ret = mtd_device_unregister(&flash->mtd);
  860. if (ret)
  861. dev_err(&pdev->dev, "error removing mtd\n");
  862. iounmap(flash->base_addr);
  863. kfree(flash);
  864. }
  865. irq = platform_get_irq(pdev, 0);
  866. free_irq(irq, dev);
  867. clk_disable(dev->clk);
  868. clk_put(dev->clk);
  869. iounmap(dev->io_base);
  870. kfree(dev);
  871. smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  872. release_mem_region(smi_base->start, resource_size(smi_base));
  873. platform_set_drvdata(pdev, NULL);
  874. return 0;
  875. }
  876. int spear_smi_suspend(struct platform_device *pdev, pm_message_t state)
  877. {
  878. struct spear_smi *dev = platform_get_drvdata(pdev);
  879. if (dev && dev->clk)
  880. clk_disable(dev->clk);
  881. return 0;
  882. }
  883. int spear_smi_resume(struct platform_device *pdev)
  884. {
  885. struct spear_smi *dev = platform_get_drvdata(pdev);
  886. int ret = -EPERM;
  887. if (dev && dev->clk)
  888. ret = clk_enable(dev->clk);
  889. if (!ret)
  890. spear_smi_hw_init(dev);
  891. return ret;
  892. }
  893. static struct platform_driver spear_smi_driver = {
  894. .driver = {
  895. .name = "smi",
  896. .bus = &platform_bus_type,
  897. .owner = THIS_MODULE,
  898. },
  899. .probe = spear_smi_probe,
  900. .remove = __devexit_p(spear_smi_remove),
  901. .suspend = spear_smi_suspend,
  902. .resume = spear_smi_resume,
  903. };
  904. static int spear_smi_init(void)
  905. {
  906. return platform_driver_register(&spear_smi_driver);
  907. }
  908. module_init(spear_smi_init);
  909. static void spear_smi_exit(void)
  910. {
  911. platform_driver_unregister(&spear_smi_driver);
  912. }
  913. module_exit(spear_smi_exit);
  914. MODULE_LICENSE("GPL");
  915. MODULE_AUTHOR("Ashish Priyadarshi, Shiraz Hashim <shiraz.hashim@st.com>");
  916. MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");