spear_smi.c 29 KB

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