i2c.c 24 KB

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