i2c.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. * I2C Link Layer for PN544 HCI based Driver
  3. *
  4. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/crc-ccitt.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/gpio.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/acpi.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/nfc.h>
  30. #include <linux/firmware.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/platform_data/pn544.h>
  33. #include <asm/unaligned.h>
  34. #include <net/nfc/hci.h>
  35. #include <net/nfc/llc.h>
  36. #include <net/nfc/nfc.h>
  37. #include "pn544.h"
  38. #define PN544_I2C_FRAME_HEADROOM 1
  39. #define PN544_I2C_FRAME_TAILROOM 2
  40. /* GPIO names */
  41. #define PN544_GPIO_NAME_IRQ "pn544_irq"
  42. #define PN544_GPIO_NAME_FW "pn544_fw"
  43. #define PN544_GPIO_NAME_EN "pn544_en"
  44. /* framing in HCI mode */
  45. #define PN544_HCI_I2C_LLC_LEN 1
  46. #define PN544_HCI_I2C_LLC_CRC 2
  47. #define PN544_HCI_I2C_LLC_LEN_CRC (PN544_HCI_I2C_LLC_LEN + \
  48. PN544_HCI_I2C_LLC_CRC)
  49. #define PN544_HCI_I2C_LLC_MIN_SIZE (1 + PN544_HCI_I2C_LLC_LEN_CRC)
  50. #define PN544_HCI_I2C_LLC_MAX_PAYLOAD 29
  51. #define PN544_HCI_I2C_LLC_MAX_SIZE (PN544_HCI_I2C_LLC_LEN_CRC + 1 + \
  52. PN544_HCI_I2C_LLC_MAX_PAYLOAD)
  53. static struct i2c_device_id pn544_hci_i2c_id_table[] = {
  54. {"pn544", 0},
  55. {}
  56. };
  57. MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
  58. static const struct acpi_device_id pn544_hci_i2c_acpi_match[] = {
  59. {"NXP5440", 0},
  60. {}
  61. };
  62. MODULE_DEVICE_TABLE(acpi, pn544_hci_i2c_acpi_match);
  63. #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
  64. /*
  65. * Exposed through the 4 most significant bytes
  66. * from the HCI SW_VERSION first byte, a.k.a.
  67. * SW RomLib.
  68. */
  69. #define PN544_HW_VARIANT_C2 0xa
  70. #define PN544_HW_VARIANT_C3 0xb
  71. #define PN544_FW_CMD_RESET 0x01
  72. #define PN544_FW_CMD_WRITE 0x08
  73. #define PN544_FW_CMD_CHECK 0x06
  74. #define PN544_FW_CMD_SECURE_WRITE 0x0C
  75. #define PN544_FW_CMD_SECURE_CHUNK_WRITE 0x0D
  76. struct pn544_i2c_fw_frame_write {
  77. u8 cmd;
  78. u16 be_length;
  79. u8 be_dest_addr[3];
  80. u16 be_datalen;
  81. u8 data[];
  82. } __packed;
  83. struct pn544_i2c_fw_frame_check {
  84. u8 cmd;
  85. u16 be_length;
  86. u8 be_start_addr[3];
  87. u16 be_datalen;
  88. u16 be_crc;
  89. } __packed;
  90. struct pn544_i2c_fw_frame_response {
  91. u8 status;
  92. u16 be_length;
  93. } __packed;
  94. struct pn544_i2c_fw_blob {
  95. u32 be_size;
  96. u32 be_destaddr;
  97. u8 data[];
  98. };
  99. struct pn544_i2c_fw_secure_frame {
  100. u8 cmd;
  101. u16 be_datalen;
  102. u8 data[];
  103. } __packed;
  104. struct pn544_i2c_fw_secure_blob {
  105. u64 header;
  106. u8 data[];
  107. };
  108. #define PN544_FW_CMD_RESULT_TIMEOUT 0x01
  109. #define PN544_FW_CMD_RESULT_BAD_CRC 0x02
  110. #define PN544_FW_CMD_RESULT_ACCESS_DENIED 0x08
  111. #define PN544_FW_CMD_RESULT_PROTOCOL_ERROR 0x0B
  112. #define PN544_FW_CMD_RESULT_INVALID_PARAMETER 0x11
  113. #define PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND 0x13
  114. #define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18
  115. #define PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR 0x19
  116. #define PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR 0x1D
  117. #define PN544_FW_CMD_RESULT_MEMORY_ERROR 0x20
  118. #define PN544_FW_CMD_RESULT_CHUNK_OK 0x21
  119. #define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74
  120. #define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0
  121. #define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6
  122. #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
  123. #define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7
  124. #define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE
  125. #define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8
  126. #define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\
  127. PN544_FW_I2C_WRITE_FRAME_HEADER_LEN),\
  128. PN544_FW_WRITE_BUFFER_MAX_LEN)
  129. #define PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN 3
  130. #define PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN (PN544_FW_I2C_MAX_PAYLOAD -\
  131. PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN)
  132. #define PN544_FW_SECURE_FRAME_HEADER_LEN 3
  133. #define PN544_FW_SECURE_BLOB_HEADER_LEN 8
  134. #define FW_WORK_STATE_IDLE 1
  135. #define FW_WORK_STATE_START 2
  136. #define FW_WORK_STATE_WAIT_WRITE_ANSWER 3
  137. #define FW_WORK_STATE_WAIT_CHECK_ANSWER 4
  138. #define FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER 5
  139. struct pn544_i2c_phy {
  140. struct i2c_client *i2c_dev;
  141. struct nfc_hci_dev *hdev;
  142. unsigned int gpio_en;
  143. unsigned int gpio_fw;
  144. unsigned int en_polarity;
  145. u8 hw_variant;
  146. struct work_struct fw_work;
  147. int fw_work_state;
  148. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  149. const struct firmware *fw;
  150. u32 fw_blob_dest_addr;
  151. size_t fw_blob_size;
  152. const u8 *fw_blob_data;
  153. size_t fw_written;
  154. size_t fw_size;
  155. int fw_cmd_result;
  156. int powered;
  157. int run_mode;
  158. int hard_fault; /*
  159. * < 0 if hardware error occured (e.g. i2c err)
  160. * and prevents normal operation.
  161. */
  162. };
  163. #define I2C_DUMP_SKB(info, skb) \
  164. do { \
  165. pr_debug("%s:\n", info); \
  166. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  167. 16, 1, (skb)->data, (skb)->len, 0); \
  168. } while (0)
  169. static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy)
  170. {
  171. int polarity, retry, ret;
  172. char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
  173. int count = sizeof(rset_cmd);
  174. nfc_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n");
  175. /* Disable fw download */
  176. gpio_set_value_cansleep(phy->gpio_fw, 0);
  177. for (polarity = 0; polarity < 2; polarity++) {
  178. phy->en_polarity = polarity;
  179. retry = 3;
  180. while (retry--) {
  181. /* power off */
  182. gpio_set_value_cansleep(phy->gpio_en,
  183. !phy->en_polarity);
  184. usleep_range(10000, 15000);
  185. /* power on */
  186. gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
  187. usleep_range(10000, 15000);
  188. /* send reset */
  189. dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n");
  190. ret = i2c_master_send(phy->i2c_dev, rset_cmd, count);
  191. if (ret == count) {
  192. nfc_info(&phy->i2c_dev->dev,
  193. "nfc_en polarity : active %s\n",
  194. (polarity == 0 ? "low" : "high"));
  195. goto out;
  196. }
  197. }
  198. }
  199. nfc_err(&phy->i2c_dev->dev,
  200. "Could not detect nfc_en polarity, fallback to active high\n");
  201. out:
  202. gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
  203. }
  204. static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy *phy, int run_mode)
  205. {
  206. gpio_set_value_cansleep(phy->gpio_fw,
  207. run_mode == PN544_FW_MODE ? 1 : 0);
  208. gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
  209. usleep_range(10000, 15000);
  210. phy->run_mode = run_mode;
  211. }
  212. static int pn544_hci_i2c_enable(void *phy_id)
  213. {
  214. struct pn544_i2c_phy *phy = phy_id;
  215. pr_info("%s\n", __func__);
  216. pn544_hci_i2c_enable_mode(phy, PN544_HCI_MODE);
  217. phy->powered = 1;
  218. return 0;
  219. }
  220. static void pn544_hci_i2c_disable(void *phy_id)
  221. {
  222. struct pn544_i2c_phy *phy = phy_id;
  223. gpio_set_value_cansleep(phy->gpio_fw, 0);
  224. gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
  225. usleep_range(10000, 15000);
  226. gpio_set_value_cansleep(phy->gpio_en, phy->en_polarity);
  227. usleep_range(10000, 15000);
  228. gpio_set_value_cansleep(phy->gpio_en, !phy->en_polarity);
  229. usleep_range(10000, 15000);
  230. phy->powered = 0;
  231. }
  232. static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb)
  233. {
  234. u16 crc;
  235. int len;
  236. len = skb->len + 2;
  237. *skb_push(skb, 1) = len;
  238. crc = crc_ccitt(0xffff, skb->data, skb->len);
  239. crc = ~crc;
  240. *skb_put(skb, 1) = crc & 0xff;
  241. *skb_put(skb, 1) = crc >> 8;
  242. }
  243. static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb)
  244. {
  245. skb_pull(skb, PN544_I2C_FRAME_HEADROOM);
  246. skb_trim(skb, PN544_I2C_FRAME_TAILROOM);
  247. }
  248. /*
  249. * Writing a frame must not return the number of written bytes.
  250. * It must return either zero for success, or <0 for error.
  251. * In addition, it must not alter the skb
  252. */
  253. static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb)
  254. {
  255. int r;
  256. struct pn544_i2c_phy *phy = phy_id;
  257. struct i2c_client *client = phy->i2c_dev;
  258. if (phy->hard_fault != 0)
  259. return phy->hard_fault;
  260. usleep_range(3000, 6000);
  261. pn544_hci_i2c_add_len_crc(skb);
  262. I2C_DUMP_SKB("i2c frame written", skb);
  263. r = i2c_master_send(client, skb->data, skb->len);
  264. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  265. usleep_range(6000, 10000);
  266. r = i2c_master_send(client, skb->data, skb->len);
  267. }
  268. if (r >= 0) {
  269. if (r != skb->len)
  270. r = -EREMOTEIO;
  271. else
  272. r = 0;
  273. }
  274. pn544_hci_i2c_remove_len_crc(skb);
  275. return r;
  276. }
  277. static int check_crc(u8 *buf, int buflen)
  278. {
  279. int len;
  280. u16 crc;
  281. len = buf[0] + 1;
  282. crc = crc_ccitt(0xffff, buf, len - 2);
  283. crc = ~crc;
  284. if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
  285. pr_err("CRC error 0x%x != 0x%x 0x%x\n",
  286. crc, buf[len - 1], buf[len - 2]);
  287. pr_info("%s: BAD CRC\n", __func__);
  288. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  289. 16, 2, buf, buflen, false);
  290. return -EPERM;
  291. }
  292. return 0;
  293. }
  294. /*
  295. * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
  296. * that i2c bus will be flushed and that next read will start on a new frame.
  297. * returned skb contains only LLC header and payload.
  298. * returns:
  299. * -EREMOTEIO : i2c read error (fatal)
  300. * -EBADMSG : frame was incorrect and discarded
  301. * -ENOMEM : cannot allocate skb, frame dropped
  302. */
  303. static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb)
  304. {
  305. int r;
  306. u8 len;
  307. u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1];
  308. struct i2c_client *client = phy->i2c_dev;
  309. r = i2c_master_recv(client, &len, 1);
  310. if (r != 1) {
  311. nfc_err(&client->dev, "cannot read len byte\n");
  312. return -EREMOTEIO;
  313. }
  314. if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) ||
  315. (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) {
  316. nfc_err(&client->dev, "invalid len byte\n");
  317. r = -EBADMSG;
  318. goto flush;
  319. }
  320. *skb = alloc_skb(1 + len, GFP_KERNEL);
  321. if (*skb == NULL) {
  322. r = -ENOMEM;
  323. goto flush;
  324. }
  325. *skb_put(*skb, 1) = len;
  326. r = i2c_master_recv(client, skb_put(*skb, len), len);
  327. if (r != len) {
  328. kfree_skb(*skb);
  329. return -EREMOTEIO;
  330. }
  331. I2C_DUMP_SKB("i2c frame read", *skb);
  332. r = check_crc((*skb)->data, (*skb)->len);
  333. if (r != 0) {
  334. kfree_skb(*skb);
  335. r = -EBADMSG;
  336. goto flush;
  337. }
  338. skb_pull(*skb, 1);
  339. skb_trim(*skb, (*skb)->len - 2);
  340. usleep_range(3000, 6000);
  341. return 0;
  342. flush:
  343. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  344. r = -EREMOTEIO;
  345. usleep_range(3000, 6000);
  346. return r;
  347. }
  348. static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy *phy)
  349. {
  350. int r;
  351. struct pn544_i2c_fw_frame_response response;
  352. struct i2c_client *client = phy->i2c_dev;
  353. r = i2c_master_recv(client, (char *) &response, sizeof(response));
  354. if (r != sizeof(response)) {
  355. nfc_err(&client->dev, "cannot read fw status\n");
  356. return -EIO;
  357. }
  358. usleep_range(3000, 6000);
  359. switch (response.status) {
  360. case 0:
  361. return 0;
  362. case PN544_FW_CMD_RESULT_CHUNK_OK:
  363. return response.status;
  364. case PN544_FW_CMD_RESULT_TIMEOUT:
  365. return -ETIMEDOUT;
  366. case PN544_FW_CMD_RESULT_BAD_CRC:
  367. return -ENODATA;
  368. case PN544_FW_CMD_RESULT_ACCESS_DENIED:
  369. return -EACCES;
  370. case PN544_FW_CMD_RESULT_PROTOCOL_ERROR:
  371. return -EPROTO;
  372. case PN544_FW_CMD_RESULT_INVALID_PARAMETER:
  373. return -EINVAL;
  374. case PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND:
  375. return -ENOTSUPP;
  376. case PN544_FW_CMD_RESULT_INVALID_LENGTH:
  377. return -EBADMSG;
  378. case PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR:
  379. return -ENOKEY;
  380. case PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR:
  381. return -EINVAL;
  382. case PN544_FW_CMD_RESULT_MEMORY_ERROR:
  383. return -ENOMEM;
  384. case PN544_FW_CMD_RESULT_COMMAND_REJECTED:
  385. return -EACCES;
  386. case PN544_FW_CMD_RESULT_WRITE_FAILED:
  387. case PN544_FW_CMD_RESULT_CHUNK_ERROR:
  388. return -EIO;
  389. default:
  390. return -EIO;
  391. }
  392. }
  393. /*
  394. * Reads an shdlc frame from the chip. This is not as straightforward as it
  395. * seems. There are cases where we could loose the frame start synchronization.
  396. * The frame format is len-data-crc, and corruption can occur anywhere while
  397. * transiting on i2c bus, such that we could read an invalid len.
  398. * In order to recover synchronization with the next frame, we must be sure
  399. * to read the real amount of data without using the len byte. We do this by
  400. * assuming the following:
  401. * - the chip will always present only one single complete frame on the bus
  402. * before triggering the interrupt
  403. * - the chip will not present a new frame until we have completely read
  404. * the previous one (or until we have handled the interrupt).
  405. * The tricky case is when we read a corrupted len that is less than the real
  406. * len. We must detect this here in order to determine that we need to flush
  407. * the bus. This is the reason why we check the crc here.
  408. */
  409. static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id)
  410. {
  411. struct pn544_i2c_phy *phy = phy_id;
  412. struct i2c_client *client;
  413. struct sk_buff *skb = NULL;
  414. int r;
  415. if (!phy || irq != phy->i2c_dev->irq) {
  416. WARN_ON_ONCE(1);
  417. return IRQ_NONE;
  418. }
  419. client = phy->i2c_dev;
  420. dev_dbg(&client->dev, "IRQ\n");
  421. if (phy->hard_fault != 0)
  422. return IRQ_HANDLED;
  423. if (phy->run_mode == PN544_FW_MODE) {
  424. phy->fw_cmd_result = pn544_hci_i2c_fw_read_status(phy);
  425. schedule_work(&phy->fw_work);
  426. } else {
  427. r = pn544_hci_i2c_read(phy, &skb);
  428. if (r == -EREMOTEIO) {
  429. phy->hard_fault = r;
  430. nfc_hci_recv_frame(phy->hdev, NULL);
  431. return IRQ_HANDLED;
  432. } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
  433. return IRQ_HANDLED;
  434. }
  435. nfc_hci_recv_frame(phy->hdev, skb);
  436. }
  437. return IRQ_HANDLED;
  438. }
  439. static struct nfc_phy_ops i2c_phy_ops = {
  440. .write = pn544_hci_i2c_write,
  441. .enable = pn544_hci_i2c_enable,
  442. .disable = pn544_hci_i2c_disable,
  443. };
  444. static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name,
  445. u8 hw_variant)
  446. {
  447. struct pn544_i2c_phy *phy = phy_id;
  448. pr_info("Starting Firmware Download (%s)\n", firmware_name);
  449. strcpy(phy->firmware_name, firmware_name);
  450. phy->hw_variant = hw_variant;
  451. phy->fw_work_state = FW_WORK_STATE_START;
  452. schedule_work(&phy->fw_work);
  453. return 0;
  454. }
  455. static void pn544_hci_i2c_fw_work_complete(struct pn544_i2c_phy *phy,
  456. int result)
  457. {
  458. pr_info("Firmware Download Complete, result=%d\n", result);
  459. pn544_hci_i2c_disable(phy);
  460. phy->fw_work_state = FW_WORK_STATE_IDLE;
  461. if (phy->fw) {
  462. release_firmware(phy->fw);
  463. phy->fw = NULL;
  464. }
  465. nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result);
  466. }
  467. static int pn544_hci_i2c_fw_write_cmd(struct i2c_client *client, u32 dest_addr,
  468. const u8 *data, u16 datalen)
  469. {
  470. u8 frame[PN544_FW_I2C_MAX_PAYLOAD];
  471. struct pn544_i2c_fw_frame_write *framep;
  472. u16 params_len;
  473. int framelen;
  474. int r;
  475. if (datalen > PN544_FW_I2C_WRITE_DATA_MAX_LEN)
  476. datalen = PN544_FW_I2C_WRITE_DATA_MAX_LEN;
  477. framep = (struct pn544_i2c_fw_frame_write *) frame;
  478. params_len = sizeof(framep->be_dest_addr) +
  479. sizeof(framep->be_datalen) + datalen;
  480. framelen = params_len + sizeof(framep->cmd) +
  481. sizeof(framep->be_length);
  482. framep->cmd = PN544_FW_CMD_WRITE;
  483. put_unaligned_be16(params_len, &framep->be_length);
  484. framep->be_dest_addr[0] = (dest_addr & 0xff0000) >> 16;
  485. framep->be_dest_addr[1] = (dest_addr & 0xff00) >> 8;
  486. framep->be_dest_addr[2] = dest_addr & 0xff;
  487. put_unaligned_be16(datalen, &framep->be_datalen);
  488. memcpy(framep->data, data, datalen);
  489. r = i2c_master_send(client, frame, framelen);
  490. if (r == framelen)
  491. return datalen;
  492. else if (r < 0)
  493. return r;
  494. else
  495. return -EIO;
  496. }
  497. static int pn544_hci_i2c_fw_check_cmd(struct i2c_client *client, u32 start_addr,
  498. const u8 *data, u16 datalen)
  499. {
  500. struct pn544_i2c_fw_frame_check frame;
  501. int r;
  502. u16 crc;
  503. /* calculate local crc for the data we want to check */
  504. crc = crc_ccitt(0xffff, data, datalen);
  505. frame.cmd = PN544_FW_CMD_CHECK;
  506. put_unaligned_be16(sizeof(frame.be_start_addr) +
  507. sizeof(frame.be_datalen) + sizeof(frame.be_crc),
  508. &frame.be_length);
  509. /* tell the chip the memory region to which our crc applies */
  510. frame.be_start_addr[0] = (start_addr & 0xff0000) >> 16;
  511. frame.be_start_addr[1] = (start_addr & 0xff00) >> 8;
  512. frame.be_start_addr[2] = start_addr & 0xff;
  513. put_unaligned_be16(datalen, &frame.be_datalen);
  514. /*
  515. * and give our local crc. Chip will calculate its own crc for the
  516. * region and compare with ours.
  517. */
  518. put_unaligned_be16(crc, &frame.be_crc);
  519. r = i2c_master_send(client, (const char *) &frame, sizeof(frame));
  520. if (r == sizeof(frame))
  521. return 0;
  522. else if (r < 0)
  523. return r;
  524. else
  525. return -EIO;
  526. }
  527. static int pn544_hci_i2c_fw_write_chunk(struct pn544_i2c_phy *phy)
  528. {
  529. int r;
  530. r = pn544_hci_i2c_fw_write_cmd(phy->i2c_dev,
  531. phy->fw_blob_dest_addr + phy->fw_written,
  532. phy->fw_blob_data + phy->fw_written,
  533. phy->fw_blob_size - phy->fw_written);
  534. if (r < 0)
  535. return r;
  536. phy->fw_written += r;
  537. phy->fw_work_state = FW_WORK_STATE_WAIT_WRITE_ANSWER;
  538. return 0;
  539. }
  540. static int pn544_hci_i2c_fw_secure_write_frame_cmd(struct pn544_i2c_phy *phy,
  541. const u8 *data, u16 datalen)
  542. {
  543. u8 buf[PN544_FW_I2C_MAX_PAYLOAD];
  544. struct pn544_i2c_fw_secure_frame *chunk;
  545. int chunklen;
  546. int r;
  547. if (datalen > PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN)
  548. datalen = PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN;
  549. chunk = (struct pn544_i2c_fw_secure_frame *) buf;
  550. chunk->cmd = PN544_FW_CMD_SECURE_CHUNK_WRITE;
  551. put_unaligned_be16(datalen, &chunk->be_datalen);
  552. memcpy(chunk->data, data, datalen);
  553. chunklen = sizeof(chunk->cmd) + sizeof(chunk->be_datalen) + datalen;
  554. r = i2c_master_send(phy->i2c_dev, buf, chunklen);
  555. if (r == chunklen)
  556. return datalen;
  557. else if (r < 0)
  558. return r;
  559. else
  560. return -EIO;
  561. }
  562. static int pn544_hci_i2c_fw_secure_write_frame(struct pn544_i2c_phy *phy)
  563. {
  564. struct pn544_i2c_fw_secure_frame *framep;
  565. int r;
  566. framep = (struct pn544_i2c_fw_secure_frame *) phy->fw_blob_data;
  567. if (phy->fw_written == 0)
  568. phy->fw_blob_size = get_unaligned_be16(&framep->be_datalen)
  569. + PN544_FW_SECURE_FRAME_HEADER_LEN;
  570. /* Only secure write command can be chunked*/
  571. if (phy->fw_blob_size > PN544_FW_I2C_MAX_PAYLOAD &&
  572. framep->cmd != PN544_FW_CMD_SECURE_WRITE)
  573. return -EINVAL;
  574. /* The firmware also have other commands, we just send them directly */
  575. if (phy->fw_blob_size < PN544_FW_I2C_MAX_PAYLOAD) {
  576. r = i2c_master_send(phy->i2c_dev,
  577. (const char *) phy->fw_blob_data, phy->fw_blob_size);
  578. if (r == phy->fw_blob_size)
  579. goto exit;
  580. else if (r < 0)
  581. return r;
  582. else
  583. return -EIO;
  584. }
  585. r = pn544_hci_i2c_fw_secure_write_frame_cmd(phy,
  586. phy->fw_blob_data + phy->fw_written,
  587. phy->fw_blob_size - phy->fw_written);
  588. if (r < 0)
  589. return r;
  590. exit:
  591. phy->fw_written += r;
  592. phy->fw_work_state = FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER;
  593. /* SW reset command will not trig any response from PN544 */
  594. if (framep->cmd == PN544_FW_CMD_RESET) {
  595. pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
  596. phy->fw_cmd_result = 0;
  597. schedule_work(&phy->fw_work);
  598. }
  599. return 0;
  600. }
  601. static void pn544_hci_i2c_fw_work(struct work_struct *work)
  602. {
  603. struct pn544_i2c_phy *phy = container_of(work, struct pn544_i2c_phy,
  604. fw_work);
  605. int r;
  606. struct pn544_i2c_fw_blob *blob;
  607. struct pn544_i2c_fw_secure_blob *secure_blob;
  608. switch (phy->fw_work_state) {
  609. case FW_WORK_STATE_START:
  610. pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
  611. r = reject_firmware(&phy->fw, phy->firmware_name,
  612. &phy->i2c_dev->dev);
  613. if (r < 0)
  614. goto exit_state_start;
  615. phy->fw_written = 0;
  616. switch (phy->hw_variant) {
  617. case PN544_HW_VARIANT_C2:
  618. blob = (struct pn544_i2c_fw_blob *) phy->fw->data;
  619. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  620. phy->fw_blob_dest_addr = get_unaligned_be32(
  621. &blob->be_destaddr);
  622. phy->fw_blob_data = blob->data;
  623. r = pn544_hci_i2c_fw_write_chunk(phy);
  624. break;
  625. case PN544_HW_VARIANT_C3:
  626. secure_blob = (struct pn544_i2c_fw_secure_blob *)
  627. phy->fw->data;
  628. phy->fw_blob_data = secure_blob->data;
  629. phy->fw_size = phy->fw->size;
  630. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  631. break;
  632. default:
  633. r = -ENOTSUPP;
  634. break;
  635. }
  636. exit_state_start:
  637. if (r < 0)
  638. pn544_hci_i2c_fw_work_complete(phy, r);
  639. break;
  640. case FW_WORK_STATE_WAIT_WRITE_ANSWER:
  641. r = phy->fw_cmd_result;
  642. if (r < 0)
  643. goto exit_state_wait_write_answer;
  644. if (phy->fw_written == phy->fw_blob_size) {
  645. r = pn544_hci_i2c_fw_check_cmd(phy->i2c_dev,
  646. phy->fw_blob_dest_addr,
  647. phy->fw_blob_data,
  648. phy->fw_blob_size);
  649. if (r < 0)
  650. goto exit_state_wait_write_answer;
  651. phy->fw_work_state = FW_WORK_STATE_WAIT_CHECK_ANSWER;
  652. break;
  653. }
  654. r = pn544_hci_i2c_fw_write_chunk(phy);
  655. exit_state_wait_write_answer:
  656. if (r < 0)
  657. pn544_hci_i2c_fw_work_complete(phy, r);
  658. break;
  659. case FW_WORK_STATE_WAIT_CHECK_ANSWER:
  660. r = phy->fw_cmd_result;
  661. if (r < 0)
  662. goto exit_state_wait_check_answer;
  663. blob = (struct pn544_i2c_fw_blob *) (phy->fw_blob_data +
  664. phy->fw_blob_size);
  665. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  666. if (phy->fw_blob_size != 0) {
  667. phy->fw_blob_dest_addr =
  668. get_unaligned_be32(&blob->be_destaddr);
  669. phy->fw_blob_data = blob->data;
  670. phy->fw_written = 0;
  671. r = pn544_hci_i2c_fw_write_chunk(phy);
  672. }
  673. exit_state_wait_check_answer:
  674. if (r < 0 || phy->fw_blob_size == 0)
  675. pn544_hci_i2c_fw_work_complete(phy, r);
  676. break;
  677. case FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER:
  678. r = phy->fw_cmd_result;
  679. if (r < 0)
  680. goto exit_state_wait_secure_write_answer;
  681. if (r == PN544_FW_CMD_RESULT_CHUNK_OK) {
  682. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  683. goto exit_state_wait_secure_write_answer;
  684. }
  685. if (phy->fw_written == phy->fw_blob_size) {
  686. secure_blob = (struct pn544_i2c_fw_secure_blob *)
  687. (phy->fw_blob_data + phy->fw_blob_size);
  688. phy->fw_size -= phy->fw_blob_size +
  689. PN544_FW_SECURE_BLOB_HEADER_LEN;
  690. if (phy->fw_size >= PN544_FW_SECURE_BLOB_HEADER_LEN
  691. + PN544_FW_SECURE_FRAME_HEADER_LEN) {
  692. phy->fw_blob_data = secure_blob->data;
  693. phy->fw_written = 0;
  694. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  695. }
  696. }
  697. exit_state_wait_secure_write_answer:
  698. if (r < 0 || phy->fw_size == 0)
  699. pn544_hci_i2c_fw_work_complete(phy, r);
  700. break;
  701. default:
  702. break;
  703. }
  704. }
  705. static int pn544_hci_i2c_acpi_request_resources(struct i2c_client *client)
  706. {
  707. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  708. struct gpio_desc *gpiod_en, *gpiod_fw;
  709. struct device *dev = &client->dev;
  710. /* Get EN GPIO from ACPI */
  711. gpiod_en = devm_gpiod_get_index(dev, PN544_GPIO_NAME_EN, 1,
  712. GPIOD_OUT_LOW);
  713. if (IS_ERR(gpiod_en)) {
  714. nfc_err(dev, "Unable to get EN GPIO\n");
  715. return -ENODEV;
  716. }
  717. phy->gpio_en = desc_to_gpio(gpiod_en);
  718. /* Get FW GPIO from ACPI */
  719. gpiod_fw = devm_gpiod_get_index(dev, PN544_GPIO_NAME_FW, 2,
  720. GPIOD_OUT_LOW);
  721. if (IS_ERR(gpiod_fw)) {
  722. nfc_err(dev, "Unable to get FW GPIO\n");
  723. return -ENODEV;
  724. }
  725. phy->gpio_fw = desc_to_gpio(gpiod_fw);
  726. return 0;
  727. }
  728. static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
  729. {
  730. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  731. struct device_node *pp;
  732. int ret;
  733. pp = client->dev.of_node;
  734. if (!pp) {
  735. ret = -ENODEV;
  736. goto err_dt;
  737. }
  738. /* Obtention of EN GPIO from device tree */
  739. ret = of_get_named_gpio(pp, "enable-gpios", 0);
  740. if (ret < 0) {
  741. if (ret != -EPROBE_DEFER)
  742. nfc_err(&client->dev,
  743. "Failed to get EN gpio, error: %d\n", ret);
  744. goto err_dt;
  745. }
  746. phy->gpio_en = ret;
  747. /* Configuration of EN GPIO */
  748. ret = gpio_request(phy->gpio_en, PN544_GPIO_NAME_EN);
  749. if (ret) {
  750. nfc_err(&client->dev, "Fail EN pin\n");
  751. goto err_dt;
  752. }
  753. ret = gpio_direction_output(phy->gpio_en, 0);
  754. if (ret) {
  755. nfc_err(&client->dev, "Fail EN pin direction\n");
  756. goto err_gpio_en;
  757. }
  758. /* Obtention of FW GPIO from device tree */
  759. ret = of_get_named_gpio(pp, "firmware-gpios", 0);
  760. if (ret < 0) {
  761. if (ret != -EPROBE_DEFER)
  762. nfc_err(&client->dev,
  763. "Failed to get FW gpio, error: %d\n", ret);
  764. goto err_gpio_en;
  765. }
  766. phy->gpio_fw = ret;
  767. /* Configuration of FW GPIO */
  768. ret = gpio_request(phy->gpio_fw, PN544_GPIO_NAME_FW);
  769. if (ret) {
  770. nfc_err(&client->dev, "Fail FW pin\n");
  771. goto err_gpio_en;
  772. }
  773. ret = gpio_direction_output(phy->gpio_fw, 0);
  774. if (ret) {
  775. nfc_err(&client->dev, "Fail FW pin direction\n");
  776. goto err_gpio_fw;
  777. }
  778. return 0;
  779. err_gpio_fw:
  780. gpio_free(phy->gpio_fw);
  781. err_gpio_en:
  782. gpio_free(phy->gpio_en);
  783. err_dt:
  784. return ret;
  785. }
  786. static int pn544_hci_i2c_probe(struct i2c_client *client,
  787. const struct i2c_device_id *id)
  788. {
  789. struct pn544_i2c_phy *phy;
  790. struct pn544_nfc_platform_data *pdata;
  791. int r = 0;
  792. dev_dbg(&client->dev, "%s\n", __func__);
  793. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  794. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  795. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  796. return -ENODEV;
  797. }
  798. phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
  799. GFP_KERNEL);
  800. if (!phy)
  801. return -ENOMEM;
  802. INIT_WORK(&phy->fw_work, pn544_hci_i2c_fw_work);
  803. phy->fw_work_state = FW_WORK_STATE_IDLE;
  804. phy->i2c_dev = client;
  805. i2c_set_clientdata(client, phy);
  806. pdata = client->dev.platform_data;
  807. /* No platform data, using device tree. */
  808. if (!pdata && client->dev.of_node) {
  809. r = pn544_hci_i2c_of_request_resources(client);
  810. if (r) {
  811. nfc_err(&client->dev, "No DT data\n");
  812. return r;
  813. }
  814. /* Using platform data. */
  815. } else if (pdata) {
  816. if (pdata->request_resources == NULL) {
  817. nfc_err(&client->dev, "request_resources() missing\n");
  818. return -EINVAL;
  819. }
  820. r = pdata->request_resources(client);
  821. if (r) {
  822. nfc_err(&client->dev,
  823. "Cannot get platform resources\n");
  824. return r;
  825. }
  826. phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
  827. phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
  828. /* Using ACPI */
  829. } else if (ACPI_HANDLE(&client->dev)) {
  830. r = pn544_hci_i2c_acpi_request_resources(client);
  831. if (r) {
  832. nfc_err(&client->dev,
  833. "Cannot get ACPI data\n");
  834. return r;
  835. }
  836. } else {
  837. nfc_err(&client->dev, "No platform data\n");
  838. return -EINVAL;
  839. }
  840. pn544_hci_i2c_platform_init(phy);
  841. r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn,
  842. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  843. PN544_HCI_I2C_DRIVER_NAME, phy);
  844. if (r < 0) {
  845. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  846. goto err_rti;
  847. }
  848. r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
  849. PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM,
  850. PN544_HCI_I2C_LLC_MAX_PAYLOAD,
  851. pn544_hci_i2c_fw_download, &phy->hdev);
  852. if (r < 0)
  853. goto err_hci;
  854. return 0;
  855. err_hci:
  856. free_irq(client->irq, phy);
  857. err_rti:
  858. if (!pdata) {
  859. gpio_free(phy->gpio_en);
  860. gpio_free(phy->gpio_fw);
  861. } else if (pdata->free_resources) {
  862. pdata->free_resources();
  863. }
  864. return r;
  865. }
  866. static int pn544_hci_i2c_remove(struct i2c_client *client)
  867. {
  868. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  869. struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
  870. dev_dbg(&client->dev, "%s\n", __func__);
  871. cancel_work_sync(&phy->fw_work);
  872. if (phy->fw_work_state != FW_WORK_STATE_IDLE)
  873. pn544_hci_i2c_fw_work_complete(phy, -ENODEV);
  874. pn544_hci_remove(phy->hdev);
  875. if (phy->powered)
  876. pn544_hci_i2c_disable(phy);
  877. free_irq(client->irq, phy);
  878. /* No platform data, GPIOs have been requested by this driver */
  879. if (!pdata) {
  880. gpio_free(phy->gpio_en);
  881. gpio_free(phy->gpio_fw);
  882. /* Using platform data */
  883. } else if (pdata->free_resources) {
  884. pdata->free_resources();
  885. }
  886. return 0;
  887. }
  888. static const struct of_device_id of_pn544_i2c_match[] = {
  889. { .compatible = "nxp,pn544-i2c", },
  890. {},
  891. };
  892. MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
  893. static struct i2c_driver pn544_hci_i2c_driver = {
  894. .driver = {
  895. .name = PN544_HCI_I2C_DRIVER_NAME,
  896. .of_match_table = of_match_ptr(of_pn544_i2c_match),
  897. .acpi_match_table = ACPI_PTR(pn544_hci_i2c_acpi_match),
  898. },
  899. .probe = pn544_hci_i2c_probe,
  900. .id_table = pn544_hci_i2c_id_table,
  901. .remove = pn544_hci_i2c_remove,
  902. };
  903. module_i2c_driver(pn544_hci_i2c_driver);
  904. MODULE_LICENSE("GPL");
  905. MODULE_DESCRIPTION(DRIVER_DESC);