spear_smi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. mutex_unlock(&flash->lock);
  446. return ret;
  447. }
  448. addr += mtd->erasesize;
  449. len -= mtd->erasesize;
  450. }
  451. mutex_unlock(&flash->lock);
  452. return 0;
  453. }
  454. /**
  455. * spear_mtd_read - performs flash read operation as requested by the user
  456. * @mtd: MTD information of the memory bank
  457. * @from: Address from which to start read
  458. * @len: Number of bytes to be read
  459. * @retlen: Fills the Number of bytes actually read
  460. * @buf: Fills this after reading
  461. *
  462. * Read an address range from the flash chip. The address range
  463. * may be any size provided it is within the physical boundaries.
  464. * Returns 0 on success, non zero otherwise
  465. */
  466. static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  467. size_t *retlen, u8 *buf)
  468. {
  469. struct spear_snor_flash *flash = get_flash_data(mtd);
  470. struct spear_smi *dev = mtd->priv;
  471. void __iomem *src;
  472. u32 ctrlreg1, val;
  473. int ret;
  474. if (!flash || !dev)
  475. return -ENODEV;
  476. if (flash->bank > dev->num_flashes - 1) {
  477. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  478. return -EINVAL;
  479. }
  480. /* select address as per bank number */
  481. src = flash->base_addr + from;
  482. mutex_lock(&flash->lock);
  483. /* wait till previous write/erase is done. */
  484. ret = spear_smi_wait_till_ready(dev, flash->bank, SMI_MAX_TIME_OUT);
  485. if (ret) {
  486. mutex_unlock(&flash->lock);
  487. return ret;
  488. }
  489. mutex_lock(&dev->lock);
  490. /* put smi in hw mode not wbt mode */
  491. ctrlreg1 = val = readl(dev->io_base + SMI_CR1);
  492. val &= ~(SW_MODE | WB_MODE);
  493. if (flash->fast_mode)
  494. val |= FAST_MODE;
  495. writel(val, dev->io_base + SMI_CR1);
  496. memcpy_fromio(buf, src, len);
  497. /* restore ctrl reg1 */
  498. writel(ctrlreg1, dev->io_base + SMI_CR1);
  499. mutex_unlock(&dev->lock);
  500. *retlen = len;
  501. mutex_unlock(&flash->lock);
  502. return 0;
  503. }
  504. /*
  505. * The purpose of this function is to ensure a memcpy_toio() with byte writes
  506. * only. Its structure is inspired from the ARM implementation of _memcpy_toio()
  507. * which also does single byte writes but cannot be used here as this is just an
  508. * implementation detail and not part of the API. Not mentioning the comment
  509. * stating that _memcpy_toio() should be optimized.
  510. */
  511. static void spear_smi_memcpy_toio_b(volatile void __iomem *dest,
  512. const void *src, size_t len)
  513. {
  514. const unsigned char *from = src;
  515. while (len) {
  516. len--;
  517. writeb(*from, dest);
  518. from++;
  519. dest++;
  520. }
  521. }
  522. static inline int spear_smi_cpy_toio(struct spear_smi *dev, u32 bank,
  523. void __iomem *dest, const void *src, size_t len)
  524. {
  525. int ret;
  526. u32 ctrlreg1;
  527. /* wait until finished previous write command. */
  528. ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
  529. if (ret)
  530. return ret;
  531. /* put smi in write enable */
  532. ret = spear_smi_write_enable(dev, bank);
  533. if (ret)
  534. return ret;
  535. /* put smi in hw, write burst mode */
  536. mutex_lock(&dev->lock);
  537. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  538. writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1);
  539. /*
  540. * In Write Burst mode (WB_MODE), the specs states that writes must be:
  541. * - incremental
  542. * - of the same size
  543. * The ARM implementation of memcpy_toio() will optimize the number of
  544. * I/O by using as much 4-byte writes as possible, surrounded by
  545. * 2-byte/1-byte access if:
  546. * - the destination is not 4-byte aligned
  547. * - the length is not a multiple of 4-byte.
  548. * Avoid this alternance of write access size by using our own 'byte
  549. * access' helper if at least one of the two conditions above is true.
  550. */
  551. if (IS_ALIGNED(len, sizeof(u32)) &&
  552. IS_ALIGNED((uintptr_t)dest, sizeof(u32)))
  553. memcpy_toio(dest, src, len);
  554. else
  555. spear_smi_memcpy_toio_b(dest, src, len);
  556. writel(ctrlreg1, dev->io_base + SMI_CR1);
  557. mutex_unlock(&dev->lock);
  558. return 0;
  559. }
  560. /**
  561. * spear_mtd_write - performs write operation as requested by the user.
  562. * @mtd: MTD information of the memory bank.
  563. * @to: Address to write.
  564. * @len: Number of bytes to be written.
  565. * @retlen: Number of bytes actually wrote.
  566. * @buf: Buffer from which the data to be taken.
  567. *
  568. * Write an address range to the flash chip. Data must be written in
  569. * flash_page_size chunks. The address range may be any size provided
  570. * it is within the physical boundaries.
  571. * Returns 0 on success, non zero otherwise
  572. */
  573. static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  574. size_t *retlen, const u8 *buf)
  575. {
  576. struct spear_snor_flash *flash = get_flash_data(mtd);
  577. struct spear_smi *dev = mtd->priv;
  578. void __iomem *dest;
  579. u32 page_offset, page_size;
  580. int ret;
  581. if (!flash || !dev)
  582. return -ENODEV;
  583. if (flash->bank > dev->num_flashes - 1) {
  584. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  585. return -EINVAL;
  586. }
  587. /* select address as per bank number */
  588. dest = flash->base_addr + to;
  589. mutex_lock(&flash->lock);
  590. page_offset = (u32)to % flash->page_size;
  591. /* do if all the bytes fit onto one page */
  592. if (page_offset + len <= flash->page_size) {
  593. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, len);
  594. if (!ret)
  595. *retlen += len;
  596. } else {
  597. u32 i;
  598. /* the size of data remaining on the first page */
  599. page_size = flash->page_size - page_offset;
  600. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf,
  601. page_size);
  602. if (ret)
  603. goto err_write;
  604. else
  605. *retlen += page_size;
  606. /* write everything in pagesize chunks */
  607. for (i = page_size; i < len; i += page_size) {
  608. page_size = len - i;
  609. if (page_size > flash->page_size)
  610. page_size = flash->page_size;
  611. ret = spear_smi_cpy_toio(dev, flash->bank, dest + i,
  612. buf + i, page_size);
  613. if (ret)
  614. break;
  615. else
  616. *retlen += page_size;
  617. }
  618. }
  619. err_write:
  620. mutex_unlock(&flash->lock);
  621. return ret;
  622. }
  623. /**
  624. * spear_smi_probe_flash - Detects the NOR Flash chip.
  625. * @dev: structure of SMI information.
  626. * @bank: bank on which flash must be probed
  627. *
  628. * This routine will check whether there exists a flash chip on a given memory
  629. * bank ID.
  630. * Return index of the probed flash in flash devices structure
  631. */
  632. static int spear_smi_probe_flash(struct spear_smi *dev, u32 bank)
  633. {
  634. int ret;
  635. u32 val = 0;
  636. ret = spear_smi_wait_till_ready(dev, bank, SMI_PROBE_TIMEOUT);
  637. if (ret)
  638. return ret;
  639. mutex_lock(&dev->lock);
  640. dev->status = 0; /* Will be set in interrupt handler */
  641. /* put smi in sw mode */
  642. val = readl(dev->io_base + SMI_CR1);
  643. writel(val | SW_MODE, dev->io_base + SMI_CR1);
  644. /* send readid command in sw mode */
  645. writel(OPCODE_RDID, dev->io_base + SMI_TR);
  646. val = (bank << BANK_SHIFT) | SEND | (1 << TX_LEN_SHIFT) |
  647. (3 << RX_LEN_SHIFT) | TFIE;
  648. writel(val, dev->io_base + SMI_CR2);
  649. /* wait for TFF */
  650. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  651. dev->status & TFF, SMI_CMD_TIMEOUT);
  652. if (ret <= 0) {
  653. ret = -ENODEV;
  654. goto err_probe;
  655. }
  656. /* get memory chip id */
  657. val = readl(dev->io_base + SMI_RR);
  658. val &= 0x00ffffff;
  659. ret = get_flash_index(val);
  660. err_probe:
  661. /* clear sw mode */
  662. val = readl(dev->io_base + SMI_CR1);
  663. writel(val & ~SW_MODE, dev->io_base + SMI_CR1);
  664. mutex_unlock(&dev->lock);
  665. return ret;
  666. }
  667. #ifdef CONFIG_OF
  668. static int spear_smi_probe_config_dt(struct platform_device *pdev,
  669. struct device_node *np)
  670. {
  671. struct spear_smi_plat_data *pdata = dev_get_platdata(&pdev->dev);
  672. struct device_node *pp = NULL;
  673. const __be32 *addr;
  674. u32 val;
  675. int len;
  676. int i = 0;
  677. if (!np)
  678. return -ENODEV;
  679. of_property_read_u32(np, "clock-rate", &val);
  680. pdata->clk_rate = val;
  681. pdata->board_flash_info = devm_kzalloc(&pdev->dev,
  682. sizeof(*pdata->board_flash_info),
  683. GFP_KERNEL);
  684. if (!pdata->board_flash_info)
  685. return -ENOMEM;
  686. /* Fill structs for each subnode (flash device) */
  687. while ((pp = of_get_next_child(np, pp))) {
  688. struct spear_smi_flash_info *flash_info;
  689. flash_info = &pdata->board_flash_info[i];
  690. pdata->np[i] = pp;
  691. /* Read base-addr and size from DT */
  692. addr = of_get_property(pp, "reg", &len);
  693. pdata->board_flash_info->mem_base = be32_to_cpup(&addr[0]);
  694. pdata->board_flash_info->size = be32_to_cpup(&addr[1]);
  695. if (of_get_property(pp, "st,smi-fast-mode", NULL))
  696. pdata->board_flash_info->fast_mode = 1;
  697. i++;
  698. }
  699. pdata->num_flashes = i;
  700. return 0;
  701. }
  702. #else
  703. static int spear_smi_probe_config_dt(struct platform_device *pdev,
  704. struct device_node *np)
  705. {
  706. return -ENOSYS;
  707. }
  708. #endif
  709. static int spear_smi_setup_banks(struct platform_device *pdev,
  710. u32 bank, struct device_node *np)
  711. {
  712. struct spear_smi *dev = platform_get_drvdata(pdev);
  713. struct spear_smi_flash_info *flash_info;
  714. struct spear_smi_plat_data *pdata;
  715. struct spear_snor_flash *flash;
  716. struct mtd_partition *parts = NULL;
  717. int count = 0;
  718. int flash_index;
  719. int ret = 0;
  720. pdata = dev_get_platdata(&pdev->dev);
  721. if (bank > pdata->num_flashes - 1)
  722. return -EINVAL;
  723. flash_info = &pdata->board_flash_info[bank];
  724. if (!flash_info)
  725. return -ENODEV;
  726. flash = devm_kzalloc(&pdev->dev, sizeof(*flash), GFP_ATOMIC);
  727. if (!flash)
  728. return -ENOMEM;
  729. flash->bank = bank;
  730. flash->fast_mode = flash_info->fast_mode ? 1 : 0;
  731. mutex_init(&flash->lock);
  732. /* verify whether nor flash is really present on board */
  733. flash_index = spear_smi_probe_flash(dev, bank);
  734. if (flash_index < 0) {
  735. dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank);
  736. return flash_index;
  737. }
  738. /* map the memory for nor flash chip */
  739. flash->base_addr = devm_ioremap(&pdev->dev, flash_info->mem_base,
  740. flash_info->size);
  741. if (!flash->base_addr)
  742. return -EIO;
  743. dev->flash[bank] = flash;
  744. flash->mtd.priv = dev;
  745. if (flash_info->name)
  746. flash->mtd.name = flash_info->name;
  747. else
  748. flash->mtd.name = flash_devices[flash_index].name;
  749. flash->mtd.dev.parent = &pdev->dev;
  750. mtd_set_of_node(&flash->mtd, np);
  751. flash->mtd.type = MTD_NORFLASH;
  752. flash->mtd.writesize = 1;
  753. flash->mtd.flags = MTD_CAP_NORFLASH;
  754. flash->mtd.size = flash_info->size;
  755. flash->mtd.erasesize = flash_devices[flash_index].sectorsize;
  756. flash->page_size = flash_devices[flash_index].pagesize;
  757. flash->mtd.writebufsize = flash->page_size;
  758. flash->erase_cmd = flash_devices[flash_index].erase_cmd;
  759. flash->mtd._erase = spear_mtd_erase;
  760. flash->mtd._read = spear_mtd_read;
  761. flash->mtd._write = spear_mtd_write;
  762. flash->dev_id = flash_devices[flash_index].device_id;
  763. dev_info(&dev->pdev->dev, "mtd .name=%s .size=%llx(%lluM)\n",
  764. flash->mtd.name, flash->mtd.size,
  765. flash->mtd.size / (1024 * 1024));
  766. dev_info(&dev->pdev->dev, ".erasesize = 0x%x(%uK)\n",
  767. flash->mtd.erasesize, flash->mtd.erasesize / 1024);
  768. #ifndef CONFIG_OF
  769. if (flash_info->partitions) {
  770. parts = flash_info->partitions;
  771. count = flash_info->nr_partitions;
  772. }
  773. #endif
  774. ret = mtd_device_register(&flash->mtd, parts, count);
  775. if (ret) {
  776. dev_err(&dev->pdev->dev, "Err MTD partition=%d\n", ret);
  777. return ret;
  778. }
  779. return 0;
  780. }
  781. /**
  782. * spear_smi_probe - Entry routine
  783. * @pdev: platform device structure
  784. *
  785. * This is the first routine which gets invoked during booting and does all
  786. * initialization/allocation work. The routine looks for available memory banks,
  787. * and do proper init for any found one.
  788. * Returns 0 on success, non zero otherwise
  789. */
  790. static int spear_smi_probe(struct platform_device *pdev)
  791. {
  792. struct device_node *np = pdev->dev.of_node;
  793. struct spear_smi_plat_data *pdata = NULL;
  794. struct spear_smi *dev;
  795. struct resource *smi_base;
  796. int irq, ret = 0;
  797. int i;
  798. if (np) {
  799. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  800. if (!pdata) {
  801. ret = -ENOMEM;
  802. goto err;
  803. }
  804. pdev->dev.platform_data = pdata;
  805. ret = spear_smi_probe_config_dt(pdev, np);
  806. if (ret) {
  807. ret = -ENODEV;
  808. dev_err(&pdev->dev, "no platform data\n");
  809. goto err;
  810. }
  811. } else {
  812. pdata = dev_get_platdata(&pdev->dev);
  813. if (!pdata) {
  814. ret = -ENODEV;
  815. dev_err(&pdev->dev, "no platform data\n");
  816. goto err;
  817. }
  818. }
  819. irq = platform_get_irq(pdev, 0);
  820. if (irq < 0) {
  821. ret = -ENODEV;
  822. dev_err(&pdev->dev, "invalid smi irq\n");
  823. goto err;
  824. }
  825. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_ATOMIC);
  826. if (!dev) {
  827. ret = -ENOMEM;
  828. goto err;
  829. }
  830. smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  831. dev->io_base = devm_ioremap_resource(&pdev->dev, smi_base);
  832. if (IS_ERR(dev->io_base)) {
  833. ret = PTR_ERR(dev->io_base);
  834. goto err;
  835. }
  836. dev->pdev = pdev;
  837. dev->clk_rate = pdata->clk_rate;
  838. if (dev->clk_rate > SMI_MAX_CLOCK_FREQ)
  839. dev->clk_rate = SMI_MAX_CLOCK_FREQ;
  840. dev->num_flashes = pdata->num_flashes;
  841. if (dev->num_flashes > MAX_NUM_FLASH_CHIP) {
  842. dev_err(&pdev->dev, "exceeding max number of flashes\n");
  843. dev->num_flashes = MAX_NUM_FLASH_CHIP;
  844. }
  845. dev->clk = devm_clk_get(&pdev->dev, NULL);
  846. if (IS_ERR(dev->clk)) {
  847. ret = PTR_ERR(dev->clk);
  848. goto err;
  849. }
  850. ret = clk_prepare_enable(dev->clk);
  851. if (ret)
  852. goto err;
  853. ret = devm_request_irq(&pdev->dev, irq, spear_smi_int_handler, 0,
  854. pdev->name, dev);
  855. if (ret) {
  856. dev_err(&dev->pdev->dev, "SMI IRQ allocation failed\n");
  857. goto err_irq;
  858. }
  859. mutex_init(&dev->lock);
  860. init_waitqueue_head(&dev->cmd_complete);
  861. spear_smi_hw_init(dev);
  862. platform_set_drvdata(pdev, dev);
  863. /* loop for each serial nor-flash which is connected to smi */
  864. for (i = 0; i < dev->num_flashes; i++) {
  865. ret = spear_smi_setup_banks(pdev, i, pdata->np[i]);
  866. if (ret) {
  867. dev_err(&dev->pdev->dev, "bank setup failed\n");
  868. goto err_irq;
  869. }
  870. }
  871. return 0;
  872. err_irq:
  873. clk_disable_unprepare(dev->clk);
  874. err:
  875. return ret;
  876. }
  877. /**
  878. * spear_smi_remove - Exit routine
  879. * @pdev: platform device structure
  880. *
  881. * free all allocations and delete the partitions.
  882. */
  883. static int spear_smi_remove(struct platform_device *pdev)
  884. {
  885. struct spear_smi *dev;
  886. struct spear_snor_flash *flash;
  887. int ret, i;
  888. dev = platform_get_drvdata(pdev);
  889. if (!dev) {
  890. dev_err(&pdev->dev, "dev is null\n");
  891. return -ENODEV;
  892. }
  893. /* clean up for all nor flash */
  894. for (i = 0; i < dev->num_flashes; i++) {
  895. flash = dev->flash[i];
  896. if (!flash)
  897. continue;
  898. /* clean up mtd stuff */
  899. ret = mtd_device_unregister(&flash->mtd);
  900. if (ret)
  901. dev_err(&pdev->dev, "error removing mtd\n");
  902. }
  903. clk_disable_unprepare(dev->clk);
  904. return 0;
  905. }
  906. #ifdef CONFIG_PM_SLEEP
  907. static int spear_smi_suspend(struct device *dev)
  908. {
  909. struct spear_smi *sdev = dev_get_drvdata(dev);
  910. if (sdev && sdev->clk)
  911. clk_disable_unprepare(sdev->clk);
  912. return 0;
  913. }
  914. static int spear_smi_resume(struct device *dev)
  915. {
  916. struct spear_smi *sdev = dev_get_drvdata(dev);
  917. int ret = -EPERM;
  918. if (sdev && sdev->clk)
  919. ret = clk_prepare_enable(sdev->clk);
  920. if (!ret)
  921. spear_smi_hw_init(sdev);
  922. return ret;
  923. }
  924. #endif
  925. static SIMPLE_DEV_PM_OPS(spear_smi_pm_ops, spear_smi_suspend, spear_smi_resume);
  926. #ifdef CONFIG_OF
  927. static const struct of_device_id spear_smi_id_table[] = {
  928. { .compatible = "st,spear600-smi" },
  929. {}
  930. };
  931. MODULE_DEVICE_TABLE(of, spear_smi_id_table);
  932. #endif
  933. static struct platform_driver spear_smi_driver = {
  934. .driver = {
  935. .name = "smi",
  936. .bus = &platform_bus_type,
  937. .of_match_table = of_match_ptr(spear_smi_id_table),
  938. .pm = &spear_smi_pm_ops,
  939. },
  940. .probe = spear_smi_probe,
  941. .remove = spear_smi_remove,
  942. };
  943. module_platform_driver(spear_smi_driver);
  944. MODULE_LICENSE("GPL");
  945. MODULE_AUTHOR("Ashish Priyadarshi, Shiraz Hashim <shiraz.linux.kernel@gmail.com>");
  946. MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");