1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Silvaco dual-role I3C master driver 4 * 5 * Copyright (C) 2020 Silvaco 6 * Author: Miquel RAYNAL <miquel.raynal@bootlin.com> 7 * Based on a work from: Conor Culhane <conor.culhane@silvaco.com> 8 */ 9 10 #include <linux/bitfield.h> 11 #include <linux/clk.h> 12 #include <linux/completion.h> 13 #include <linux/errno.h> 14 #include <linux/i3c/master.h> 15 #include <linux/interrupt.h> 16 #include <linux/iopoll.h> 17 #include <linux/list.h> 18 #include <linux/module.h> 19 #include <linux/of.h> 20 #include <linux/pinctrl/consumer.h> 21 #include <linux/platform_device.h> 22 #include <linux/pm_runtime.h> 23 24 /* Master Mode Registers */ 25 #define SVC_I3C_MCONFIG 0x000 26 #define SVC_I3C_MCONFIG_MASTER_EN BIT(0) 27 #define SVC_I3C_MCONFIG_DISTO(x) FIELD_PREP(BIT(3), (x)) 28 #define SVC_I3C_MCONFIG_HKEEP(x) FIELD_PREP(GENMASK(5, 4), (x)) 29 #define SVC_I3C_MCONFIG_ODSTOP(x) FIELD_PREP(BIT(6), (x)) 30 #define SVC_I3C_MCONFIG_PPBAUD(x) FIELD_PREP(GENMASK(11, 8), (x)) 31 #define SVC_I3C_MCONFIG_PPLOW(x) FIELD_PREP(GENMASK(15, 12), (x)) 32 #define SVC_I3C_MCONFIG_ODBAUD(x) FIELD_PREP(GENMASK(23, 16), (x)) 33 #define SVC_I3C_MCONFIG_ODHPP(x) FIELD_PREP(BIT(24), (x)) 34 #define SVC_I3C_MCONFIG_SKEW(x) FIELD_PREP(GENMASK(27, 25), (x)) 35 #define SVC_I3C_MCONFIG_SKEW_MASK GENMASK(27, 25) 36 #define SVC_I3C_MCONFIG_I2CBAUD(x) FIELD_PREP(GENMASK(31, 28), (x)) 37 38 #define SVC_I3C_MCTRL 0x084 39 #define SVC_I3C_MCTRL_REQUEST_MASK GENMASK(2, 0) 40 #define SVC_I3C_MCTRL_REQUEST_NONE 0 41 #define SVC_I3C_MCTRL_REQUEST_START_ADDR 1 42 #define SVC_I3C_MCTRL_REQUEST_STOP 2 43 #define SVC_I3C_MCTRL_REQUEST_FORCE_EXIT 6 44 #define SVC_I3C_MCTRL_REQUEST_IBI_ACKNACK 3 45 #define SVC_I3C_MCTRL_REQUEST_PROC_DAA 4 46 #define SVC_I3C_MCTRL_REQUEST_AUTO_IBI 7 47 #define SVC_I3C_MCTRL_TYPE_I3C 0 48 #define SVC_I3C_MCTRL_TYPE_I2C BIT(4) 49 #define SVC_I3C_MCTRL_TYPE_DDR BIT(5) 50 #define SVC_I3C_MCTRL_IBIRESP_AUTO 0 51 #define SVC_I3C_MCTRL_IBIRESP_ACK_WITHOUT_BYTE 0 52 #define SVC_I3C_MCTRL_IBIRESP_ACK_WITH_BYTE BIT(7) 53 #define SVC_I3C_MCTRL_IBIRESP_NACK BIT(6) 54 #define SVC_I3C_MCTRL_IBIRESP_MANUAL GENMASK(7, 6) 55 #define SVC_I3C_MCTRL_DIR(x) FIELD_PREP(BIT(8), (x)) 56 #define SVC_I3C_MCTRL_DIR_WRITE 0 57 #define SVC_I3C_MCTRL_DIR_READ 1 58 #define SVC_I3C_MCTRL_ADDR(x) FIELD_PREP(GENMASK(15, 9), (x)) 59 #define SVC_I3C_MCTRL_RDTERM(x) FIELD_PREP(GENMASK(23, 16), (x)) 60 61 #define SVC_I3C_MSTATUS 0x088 62 #define SVC_I3C_MSTATUS_STATE(x) FIELD_GET(GENMASK(2, 0), (x)) 63 #define SVC_I3C_MSTATUS_STATE_DAA(x) (SVC_I3C_MSTATUS_STATE(x) == 5) 64 #define SVC_I3C_MSTATUS_STATE_SLVREQ(x) (SVC_I3C_MSTATUS_STATE(x) == 1) 65 #define SVC_I3C_MSTATUS_STATE_IDLE(x) (SVC_I3C_MSTATUS_STATE(x) == 0) 66 #define SVC_I3C_MSTATUS_BETWEEN(x) FIELD_GET(BIT(4), (x)) 67 #define SVC_I3C_MSTATUS_NACKED(x) FIELD_GET(BIT(5), (x)) 68 #define SVC_I3C_MSTATUS_IBITYPE(x) FIELD_GET(GENMASK(7, 6), (x)) 69 #define SVC_I3C_MSTATUS_IBITYPE_IBI 1 70 #define SVC_I3C_MSTATUS_IBITYPE_MASTER_REQUEST 2 71 #define SVC_I3C_MSTATUS_IBITYPE_HOT_JOIN 3 72 #define SVC_I3C_MINT_SLVSTART BIT(8) 73 #define SVC_I3C_MINT_MCTRLDONE BIT(9) 74 #define SVC_I3C_MINT_COMPLETE BIT(10) 75 #define SVC_I3C_MINT_RXPEND BIT(11) 76 #define SVC_I3C_MINT_TXNOTFULL BIT(12) 77 #define SVC_I3C_MINT_IBIWON BIT(13) 78 #define SVC_I3C_MINT_ERRWARN BIT(15) 79 #define SVC_I3C_MSTATUS_SLVSTART(x) FIELD_GET(SVC_I3C_MINT_SLVSTART, (x)) 80 #define SVC_I3C_MSTATUS_MCTRLDONE(x) FIELD_GET(SVC_I3C_MINT_MCTRLDONE, (x)) 81 #define SVC_I3C_MSTATUS_COMPLETE(x) FIELD_GET(SVC_I3C_MINT_COMPLETE, (x)) 82 #define SVC_I3C_MSTATUS_RXPEND(x) FIELD_GET(SVC_I3C_MINT_RXPEND, (x)) 83 #define SVC_I3C_MSTATUS_TXNOTFULL(x) FIELD_GET(SVC_I3C_MINT_TXNOTFULL, (x)) 84 #define SVC_I3C_MSTATUS_IBIWON(x) FIELD_GET(SVC_I3C_MINT_IBIWON, (x)) 85 #define SVC_I3C_MSTATUS_ERRWARN(x) FIELD_GET(SVC_I3C_MINT_ERRWARN, (x)) 86 #define SVC_I3C_MSTATUS_IBIADDR(x) FIELD_GET(GENMASK(30, 24), (x)) 87 88 #define SVC_I3C_IBIRULES 0x08C 89 #define SVC_I3C_IBIRULES_ADDR(slot, addr) FIELD_PREP(GENMASK(29, 0), \ 90 ((addr) & 0x3F) << ((slot) * 6)) 91 #define SVC_I3C_IBIRULES_ADDRS 5 92 #define SVC_I3C_IBIRULES_MSB0 BIT(30) 93 #define SVC_I3C_IBIRULES_NOBYTE BIT(31) 94 #define SVC_I3C_IBIRULES_MANDBYTE 0 95 #define SVC_I3C_MINTSET 0x090 96 #define SVC_I3C_MINTCLR 0x094 97 #define SVC_I3C_MINTMASKED 0x098 98 #define SVC_I3C_MERRWARN 0x09C 99 #define SVC_I3C_MERRWARN_NACK BIT(2) 100 #define SVC_I3C_MERRWARN_CRC BIT(10) 101 #define SVC_I3C_MERRWARN_TIMEOUT BIT(20) 102 #define SVC_I3C_MDMACTRL 0x0A0 103 #define SVC_I3C_MDATACTRL 0x0AC 104 #define SVC_I3C_MDATACTRL_FLUSHTB BIT(0) 105 #define SVC_I3C_MDATACTRL_FLUSHRB BIT(1) 106 #define SVC_I3C_MDATACTRL_UNLOCK_TRIG BIT(3) 107 #define SVC_I3C_MDATACTRL_TXTRIG_FIFO_NOT_FULL GENMASK(5, 4) 108 #define SVC_I3C_MDATACTRL_RXTRIG_FIFO_NOT_EMPTY 0 109 #define SVC_I3C_MDATACTRL_RXCOUNT(x) FIELD_GET(GENMASK(28, 24), (x)) 110 #define SVC_I3C_MDATACTRL_TXCOUNT(x) FIELD_GET(GENMASK(20, 16), (x)) 111 #define SVC_I3C_MDATACTRL_TXFULL BIT(30) 112 #define SVC_I3C_MDATACTRL_RXEMPTY BIT(31) 113 114 #define SVC_I3C_MWDATAB 0x0B0 115 #define SVC_I3C_MWDATAB_END BIT(8) 116 117 #define SVC_I3C_MWDATABE 0x0B4 118 #define SVC_I3C_MWDATAH 0x0B8 119 #define SVC_I3C_MWDATAHE 0x0BC 120 #define SVC_I3C_MRDATAB 0x0C0 121 #define SVC_I3C_MRDATAH 0x0C8 122 #define SVC_I3C_MWDATAB1 0x0CC 123 #define SVC_I3C_MWMSG_SDR 0x0D0 124 #define SVC_I3C_MRMSG_SDR 0x0D4 125 #define SVC_I3C_MWMSG_DDR 0x0D8 126 #define SVC_I3C_MRMSG_DDR 0x0DC 127 128 #define SVC_I3C_MDYNADDR 0x0E4 129 #define SVC_MDYNADDR_VALID BIT(0) 130 #define SVC_MDYNADDR_ADDR(x) FIELD_PREP(GENMASK(7, 1), (x)) 131 132 #define SVC_I3C_MAX_DEVS 32 133 #define SVC_I3C_PM_TIMEOUT_MS 1000 134 135 /* This parameter depends on the implementation and may be tuned */ 136 #define SVC_I3C_FIFO_SIZE 16 137 #define SVC_I3C_PPBAUD_MAX 15 138 #define SVC_I3C_QUICK_I2C_CLK 4170000 139 140 #define SVC_I3C_EVENT_IBI GENMASK(7, 0) 141 #define SVC_I3C_EVENT_HOTJOIN BIT(31) 142 143 /* 144 * SVC_I3C_QUIRK_FIFO_EMPTY: 145 * I3C HW stalls the write transfer if the transmit FIFO becomes empty, 146 * when new data is written to FIFO, I3C HW resumes the transfer but 147 * the first transmitted data bit may have the wrong value. 148 * Workaround: 149 * Fill the FIFO in advance to prevent FIFO from becoming empty. 150 */ 151 #define SVC_I3C_QUIRK_FIFO_EMPTY BIT(0) 152 /* 153 * SVC_I3C_QUIRK_FLASE_SLVSTART: 154 * I3C HW may generate an invalid SlvStart event when emitting a STOP. 155 * If it is a true SlvStart, the MSTATUS state is SLVREQ. 156 */ 157 #define SVC_I3C_QUIRK_FALSE_SLVSTART BIT(1) 158 /* 159 * SVC_I3C_QUIRK_DAA_CORRUPT: 160 * When MCONFIG.SKEW=0 and MCONFIG.ODHPP=0, the ENTDAA transaction gets 161 * corrupted and results in a no repeated-start condition at the end of 162 * address assignment. 163 * Workaround: 164 * Set MCONFIG.SKEW to 1 before initiating the DAA process. After the DAA 165 * process is completed, return MCONFIG.SKEW to its previous value. 166 */ 167 #define SVC_I3C_QUIRK_DAA_CORRUPT BIT(2) 168 169 struct svc_i3c_cmd { 170 u8 addr; 171 union { 172 bool rnw; 173 u8 cmd; 174 u32 rnw_cmd; 175 }; 176 u8 *in; 177 const void *out; 178 unsigned int len; 179 unsigned int actual_len; 180 struct i3c_xfer *xfer; 181 bool continued; 182 }; 183 184 struct svc_i3c_xfer { 185 struct list_head node; 186 struct completion comp; 187 int ret; 188 unsigned int type; 189 unsigned int ncmds; 190 struct svc_i3c_cmd cmds[] __counted_by(ncmds); 191 }; 192 193 struct svc_i3c_regs_save { 194 u32 mconfig; 195 u32 mdynaddr; 196 }; 197 198 struct svc_i3c_drvdata { 199 u32 quirks; 200 }; 201 202 /** 203 * struct svc_i3c_master - Silvaco I3C Master structure 204 * @base: I3C master controller 205 * @dev: Corresponding device 206 * @regs: Memory mapping 207 * @saved_regs: Volatile values for PM operations 208 * @free_slots: Bit array of available slots 209 * @addrs: Array containing the dynamic addresses of each attached device 210 * @descs: Array of descriptors, one per attached device 211 * @irq: Main interrupt 212 * @num_clks: I3C clock number 213 * @fclk: Fast clock (bus) 214 * @clks: I3C clock array 215 * @xferqueue: Transfer queue structure 216 * @xferqueue.list: List member 217 * @xferqueue.cur: Current ongoing transfer 218 * @xferqueue.lock: Queue lock 219 * @ibi: IBI structure 220 * @ibi.num_slots: Number of slots available in @ibi.slots 221 * @ibi.slots: Available IBI slots 222 * @ibi.tbq_slot: To be queued IBI slot 223 * @ibi.lock: IBI lock 224 * @lock: Transfer lock, protect between IBI work thread and callbacks from master 225 * @drvdata: Driver data 226 * @enabled_events: Bit masks for enable events (IBI, HotJoin). 227 * @mctrl_config: Configuration value in SVC_I3C_MCTRL for setting speed back. 228 */ 229 struct svc_i3c_master { 230 struct i3c_master_controller base; 231 struct device *dev; 232 void __iomem *regs; 233 struct svc_i3c_regs_save saved_regs; 234 u32 free_slots; 235 u8 addrs[SVC_I3C_MAX_DEVS]; 236 struct i3c_dev_desc *descs[SVC_I3C_MAX_DEVS]; 237 int irq; 238 int num_clks; 239 struct clk *fclk; 240 struct clk_bulk_data *clks; 241 struct { 242 struct list_head list; 243 struct svc_i3c_xfer *cur; 244 /* Prevent races between transfers */ 245 spinlock_t lock; 246 } xferqueue; 247 struct { 248 unsigned int num_slots; 249 struct i3c_dev_desc **slots; 250 struct i3c_ibi_slot *tbq_slot; 251 /* Prevent races within IBI handlers */ 252 spinlock_t lock; 253 } ibi; 254 struct mutex lock; 255 const struct svc_i3c_drvdata *drvdata; 256 u32 enabled_events; 257 u32 mctrl_config; 258 }; 259 260 /** 261 * struct svc_i3c_i2c_dev_data - Device specific data 262 * @index: Index in the master tables corresponding to this device 263 * @ibi: IBI slot index in the master structure 264 * @ibi_pool: IBI pool associated to this device 265 */ 266 struct svc_i3c_i2c_dev_data { 267 u8 index; 268 int ibi; 269 struct i3c_generic_ibi_pool *ibi_pool; 270 }; 271 272 static inline bool svc_has_quirk(struct svc_i3c_master *master, u32 quirk) 273 { 274 return (master->drvdata->quirks & quirk); 275 } 276 277 static inline bool svc_has_daa_corrupt(struct svc_i3c_master *master) 278 { 279 return ((master->drvdata->quirks & SVC_I3C_QUIRK_DAA_CORRUPT) && 280 !(master->mctrl_config & 281 (SVC_I3C_MCONFIG_SKEW_MASK | SVC_I3C_MCONFIG_ODHPP(1)))); 282 } 283 284 static inline bool is_events_enabled(struct svc_i3c_master *master, u32 mask) 285 { 286 return !!(master->enabled_events & mask); 287 } 288 289 static bool svc_i3c_master_error(struct svc_i3c_master *master) 290 { 291 u32 mstatus, merrwarn; 292 293 mstatus = readl(master->regs + SVC_I3C_MSTATUS); 294 if (SVC_I3C_MSTATUS_ERRWARN(mstatus)) { 295 merrwarn = readl(master->regs + SVC_I3C_MERRWARN); 296 writel(merrwarn, master->regs + SVC_I3C_MERRWARN); 297 298 /* Ignore timeout error */ 299 if (merrwarn & SVC_I3C_MERRWARN_TIMEOUT) { 300 dev_dbg(master->dev, "Warning condition: MSTATUS 0x%08x, MERRWARN 0x%08x\n", 301 mstatus, merrwarn); 302 return false; 303 } 304 305 dev_err(master->dev, 306 "Error condition: MSTATUS 0x%08x, MERRWARN 0x%08x\n", 307 mstatus, merrwarn); 308 309 return true; 310 } 311 312 return false; 313 } 314 315 static void svc_i3c_master_enable_interrupts(struct svc_i3c_master *master, u32 mask) 316 { 317 writel(mask, master->regs + SVC_I3C_MINTSET); 318 } 319 320 static void svc_i3c_master_disable_interrupts(struct svc_i3c_master *master) 321 { 322 u32 mask = readl(master->regs + SVC_I3C_MINTSET); 323 324 writel(mask, master->regs + SVC_I3C_MINTCLR); 325 } 326 327 static void svc_i3c_master_clear_merrwarn(struct svc_i3c_master *master) 328 { 329 /* Clear pending warnings */ 330 writel(readl(master->regs + SVC_I3C_MERRWARN), 331 master->regs + SVC_I3C_MERRWARN); 332 } 333 334 static void svc_i3c_master_flush_fifo(struct svc_i3c_master *master) 335 { 336 /* Flush FIFOs */ 337 writel(SVC_I3C_MDATACTRL_FLUSHTB | SVC_I3C_MDATACTRL_FLUSHRB, 338 master->regs + SVC_I3C_MDATACTRL); 339 } 340 341 static void svc_i3c_master_reset_fifo_trigger(struct svc_i3c_master *master) 342 { 343 u32 reg; 344 345 /* Set RX and TX trigger levels, flush FIFOs */ 346 reg = SVC_I3C_MDATACTRL_FLUSHTB | 347 SVC_I3C_MDATACTRL_FLUSHRB | 348 SVC_I3C_MDATACTRL_UNLOCK_TRIG | 349 SVC_I3C_MDATACTRL_TXTRIG_FIFO_NOT_FULL | 350 SVC_I3C_MDATACTRL_RXTRIG_FIFO_NOT_EMPTY; 351 writel(reg, master->regs + SVC_I3C_MDATACTRL); 352 } 353 354 static void svc_i3c_master_reset(struct svc_i3c_master *master) 355 { 356 svc_i3c_master_clear_merrwarn(master); 357 svc_i3c_master_reset_fifo_trigger(master); 358 svc_i3c_master_disable_interrupts(master); 359 } 360 361 static inline struct svc_i3c_master * 362 to_svc_i3c_master(struct i3c_master_controller *master) 363 { 364 return container_of(master, struct svc_i3c_master, base); 365 } 366 367 static struct i3c_dev_desc * 368 svc_i3c_master_dev_from_addr(struct svc_i3c_master *master, 369 unsigned int ibiaddr) 370 { 371 int i; 372 373 for (i = 0; i < SVC_I3C_MAX_DEVS; i++) 374 if (master->addrs[i] == ibiaddr) 375 break; 376 377 if (i == SVC_I3C_MAX_DEVS) 378 return NULL; 379 380 return master->descs[i]; 381 } 382 383 static bool svc_cmd_is_read(u32 rnw_cmd, u32 type) 384 { 385 return (type == SVC_I3C_MCTRL_TYPE_DDR) ? (rnw_cmd & 0x80) : rnw_cmd; 386 } 387 388 static void svc_i3c_master_emit_force_exit(struct svc_i3c_master *master) 389 { 390 u32 reg; 391 392 writel(SVC_I3C_MCTRL_REQUEST_FORCE_EXIT, master->regs + SVC_I3C_MCTRL); 393 394 /* 395 * Not need check error here because it is never happen at hardware. 396 * IP just wait for few fclk cycle to complete DDR exit pattern. Even 397 * though fclk stop, timeout happen here, the whole data actually 398 * already finish transfer. The next command will be timeout because 399 * wrong hardware state. 400 */ 401 readl_poll_timeout_atomic(master->regs + SVC_I3C_MSTATUS, reg, 402 SVC_I3C_MSTATUS_MCTRLDONE(reg), 0, 1000); 403 404 /* 405 * This delay is necessary after the emission of a stop, otherwise eg. 406 * repeating IBIs do not get detected. There is a note in the manual 407 * about it, stating that the stop condition might not be settled 408 * correctly if a start condition follows too rapidly. 409 */ 410 udelay(1); 411 } 412 413 static void svc_i3c_master_emit_stop(struct svc_i3c_master *master) 414 { 415 writel(SVC_I3C_MCTRL_REQUEST_STOP, master->regs + SVC_I3C_MCTRL); 416 417 /* 418 * This delay is necessary after the emission of a stop, otherwise eg. 419 * repeating IBIs do not get detected. There is a note in the manual 420 * about it, stating that the stop condition might not be settled 421 * correctly if a start condition follows too rapidly. 422 */ 423 udelay(1); 424 } 425 426 static int svc_i3c_master_handle_ibi(struct svc_i3c_master *master, 427 struct i3c_dev_desc *dev) 428 { 429 struct svc_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev); 430 struct i3c_ibi_slot *slot; 431 unsigned int count; 432 u32 mdatactrl; 433 int ret, val; 434 u8 *buf; 435 436 /* 437 * Wait for transfer to complete before returning. Otherwise, the EmitStop 438 * request might be sent when the transfer is not complete. 439 */ 440 ret = readl_relaxed_poll_timeout(master->regs + SVC_I3C_MSTATUS, val, 441 SVC_I3C_MSTATUS_COMPLETE(val), 0, 1000); 442 if (ret) { 443 dev_err(master->dev, "Timeout when polling for COMPLETE\n"); 444 return ret; 445 } 446 447 slot = i3c_generic_ibi_get_free_slot(data->ibi_pool); 448 if (!slot) { 449 dev_dbg(master->dev, "No free ibi slot, drop the data\n"); 450 writel(SVC_I3C_MDATACTRL_FLUSHRB, master->regs + SVC_I3C_MDATACTRL); 451 return -ENOSPC; 452 } 453 454 slot->len = 0; 455 buf = slot->data; 456 457 while (SVC_I3C_MSTATUS_RXPEND(readl(master->regs + SVC_I3C_MSTATUS)) && 458 slot->len < dev->ibi->max_payload_len) { 459 mdatactrl = readl(master->regs + SVC_I3C_MDATACTRL); 460 count = SVC_I3C_MDATACTRL_RXCOUNT(mdatactrl); 461 count = min(count, dev->ibi->max_payload_len - slot->len); 462 readsb(master->regs + SVC_I3C_MRDATAB, buf, count); 463 slot->len += count; 464 buf += count; 465 } 466 467 /* 468 * The device may have sent more than the requested payload. Drop the 469 * extra bytes so they do not leak into the next transfer. 470 */ 471 if (SVC_I3C_MSTATUS_RXPEND(readl(master->regs + SVC_I3C_MSTATUS))) 472 writel(SVC_I3C_MDATACTRL_FLUSHRB, master->regs + SVC_I3C_MDATACTRL); 473 474 master->ibi.tbq_slot = slot; 475 476 return 0; 477 } 478 479 static int svc_i3c_master_ack_ibi(struct svc_i3c_master *master, 480 bool mandatory_byte) 481 { 482 unsigned int ibi_ack_nack; 483 u32 reg; 484 485 ibi_ack_nack = SVC_I3C_MCTRL_REQUEST_IBI_ACKNACK; 486 if (mandatory_byte) 487 ibi_ack_nack |= SVC_I3C_MCTRL_IBIRESP_ACK_WITH_BYTE; 488 else 489 ibi_ack_nack |= SVC_I3C_MCTRL_IBIRESP_ACK_WITHOUT_BYTE; 490 491 writel(ibi_ack_nack, master->regs + SVC_I3C_MCTRL); 492 493 return readl_poll_timeout_atomic(master->regs + SVC_I3C_MSTATUS, reg, 494 SVC_I3C_MSTATUS_MCTRLDONE(reg), 1, 1000); 495 496 } 497 498 static int svc_i3c_master_nack_ibi(struct svc_i3c_master *master) 499 { 500 int ret; 501 u32 reg; 502 503 writel(SVC_I3C_MCTRL_REQUEST_IBI_ACKNACK | 504 SVC_I3C_MCTRL_IBIRESP_NACK, 505 master->regs + SVC_I3C_MCTRL); 506 507 ret = readl_poll_timeout_atomic(master->regs + SVC_I3C_MSTATUS, reg, 508 SVC_I3C_MSTATUS_MCTRLDONE(reg), 1, 1000); 509 return ret; 510 } 511 512 static int svc_i3c_master_handle_ibi_won(struct svc_i3c_master *master, u32 mstatus) 513 { 514 u32 ibitype; 515 int ret = 0; 516 517 ibitype = SVC_I3C_MSTATUS_IBITYPE(mstatus); 518 519 writel(SVC_I3C_MINT_IBIWON, master->regs + SVC_I3C_MSTATUS); 520 521 /* Hardware can't auto emit NACK for hot join and master request */ 522 switch (ibitype) { 523 case SVC_I3C_MSTATUS_IBITYPE_HOT_JOIN: 524 case SVC_I3C_MSTATUS_IBITYPE_MASTER_REQUEST: 525 ret = svc_i3c_master_nack_ibi(master); 526 } 527 528 return ret; 529 } 530 531 static void svc_i3c_master_ibi_isr(struct svc_i3c_master *master) 532 { 533 struct svc_i3c_i2c_dev_data *data; 534 struct i3c_dev_desc *dev = NULL; 535 unsigned int ibitype, ibiaddr; 536 u32 status, val; 537 int ret; 538 539 /* 540 * According to I3C spec ver 1.1, 09-Jun-2021, section 5.1.2.5: 541 * 542 * The I3C Controller shall hold SCL low while the Bus is in ACK/NACK Phase of I3C/I2C 543 * transfer. But maximum stall time is 100us. The IRQs have to be disabled to prevent 544 * schedule during the whole I3C transaction, otherwise, the I3C bus timeout may happen if 545 * any irq or schedule happen during transaction. 546 */ 547 guard(spinlock)(&master->xferqueue.lock); 548 549 /* 550 * IBIWON may be set before SVC_I3C_MCTRL_REQUEST_AUTO_IBI, causing 551 * readl_relaxed_poll_timeout() to return immediately. Consequently, 552 * ibitype will be 0 since it was last updated only after the 8th SCL 553 * cycle, leading to missed client IBI handlers. 554 * 555 * A typical scenario is when IBIWON occurs and bus arbitration is lost 556 * at svc_i3c_master_i3c_xfers(). 557 * 558 * Clear SVC_I3C_MINT_IBIWON before sending SVC_I3C_MCTRL_REQUEST_AUTO_IBI. 559 */ 560 writel(SVC_I3C_MINT_IBIWON, master->regs + SVC_I3C_MSTATUS); 561 562 /* 563 * Write REQUEST_START_ADDR request to emit broadcast address for arbitration, 564 * instend of using AUTO_IBI. 565 * 566 * Using AutoIBI request may cause controller to remain in AutoIBI state when 567 * there is a glitch on SDA line (high->low->high). 568 * 1. SDA high->low, raising an interrupt to execute IBI isr. 569 * 2. SDA low->high. 570 * 3. IBI isr writes an AutoIBI request. 571 * 4. The controller will not start AutoIBI process because SDA is not low. 572 * 5. IBIWON polling times out. 573 * 6. Controller remains in AutoIBI state and doesn't accept EmitStop request. 574 */ 575 writel(SVC_I3C_MCTRL_REQUEST_START_ADDR | 576 SVC_I3C_MCTRL_TYPE_I3C | 577 SVC_I3C_MCTRL_IBIRESP_MANUAL | 578 SVC_I3C_MCTRL_DIR(SVC_I3C_MCTRL_DIR_WRITE) | 579 SVC_I3C_MCTRL_ADDR(I3C_BROADCAST_ADDR), 580 master->regs + SVC_I3C_MCTRL); 581 582 /* Wait for IBIWON, should take approximately 100us */ 583 ret = readl_relaxed_poll_timeout_atomic(master->regs + SVC_I3C_MSTATUS, val, 584 SVC_I3C_MSTATUS_IBIWON(val), 0, 100); 585 if (ret) { 586 dev_err(master->dev, "Timeout when polling for IBIWON\n"); 587 svc_i3c_master_emit_stop(master); 588 return; 589 } 590 591 status = readl(master->regs + SVC_I3C_MSTATUS); 592 ibitype = SVC_I3C_MSTATUS_IBITYPE(status); 593 ibiaddr = SVC_I3C_MSTATUS_IBIADDR(status); 594 595 /* Handle the critical responses to IBI's */ 596 switch (ibitype) { 597 case SVC_I3C_MSTATUS_IBITYPE_IBI: 598 dev = svc_i3c_master_dev_from_addr(master, ibiaddr); 599 if (!dev || !is_events_enabled(master, SVC_I3C_EVENT_IBI)) { 600 svc_i3c_master_nack_ibi(master); 601 } else { 602 if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD) 603 svc_i3c_master_ack_ibi(master, true); 604 else 605 svc_i3c_master_ack_ibi(master, false); 606 svc_i3c_master_handle_ibi(master, dev); 607 } 608 break; 609 case SVC_I3C_MSTATUS_IBITYPE_HOT_JOIN: 610 if (is_events_enabled(master, SVC_I3C_EVENT_HOTJOIN)) 611 svc_i3c_master_ack_ibi(master, false); 612 else 613 svc_i3c_master_nack_ibi(master); 614 break; 615 case SVC_I3C_MSTATUS_IBITYPE_MASTER_REQUEST: 616 svc_i3c_master_nack_ibi(master); 617 break; 618 default: 619 break; 620 } 621 622 /* 623 * If an error happened, we probably got interrupted and the exchange 624 * timedout. In this case we just drop everything, emit a stop and wait 625 * for the slave to interrupt again. 626 */ 627 if (svc_i3c_master_error(master)) { 628 if (master->ibi.tbq_slot && dev) { 629 data = i3c_dev_get_master_data(dev); 630 i3c_generic_ibi_recycle_slot(data->ibi_pool, 631 master->ibi.tbq_slot); 632 master->ibi.tbq_slot = NULL; 633 } 634 635 svc_i3c_master_emit_stop(master); 636 637 return; 638 } 639 640 /* Handle the non critical tasks */ 641 switch (ibitype) { 642 case SVC_I3C_MSTATUS_IBITYPE_IBI: 643 svc_i3c_master_emit_stop(master); 644 if (dev) { 645 i3c_master_queue_ibi(dev, master->ibi.tbq_slot); 646 master->ibi.tbq_slot = NULL; 647 } 648 break; 649 case SVC_I3C_MSTATUS_IBITYPE_HOT_JOIN: 650 svc_i3c_master_emit_stop(master); 651 if (is_events_enabled(master, SVC_I3C_EVENT_HOTJOIN)) 652 i3c_master_queue_hotjoin(&master->base); 653 break; 654 case SVC_I3C_MSTATUS_IBITYPE_MASTER_REQUEST: 655 svc_i3c_master_emit_stop(master); 656 657 /* 658 * If a target gets stuck holding SDA low, the controller reports a MR. 659 * On NPCM845, emitting STOP may spuriously set SLVSTART, retriggering 660 * the interrupt and re-entering MR handling, leading to an IRQ storm. 661 * Clear SLVSTART after STOP to break the loop. 662 */ 663 if (svc_has_quirk(master, SVC_I3C_QUIRK_FALSE_SLVSTART)) 664 writel(SVC_I3C_MINT_SLVSTART, master->regs + SVC_I3C_MSTATUS); 665 break; 666 default: 667 break; 668 } 669 } 670 671 static irqreturn_t svc_i3c_master_irq_handler(int irq, void *dev_id) 672 { 673 struct svc_i3c_master *master = (struct svc_i3c_master *)dev_id; 674 u32 active = readl(master->regs + SVC_I3C_MSTATUS); 675 676 if (!SVC_I3C_MSTATUS_SLVSTART(active)) 677 return IRQ_NONE; 678 679 /* Clear the interrupt status */ 680 writel(SVC_I3C_MINT_SLVSTART, master->regs + SVC_I3C_MSTATUS); 681 682 if (svc_has_quirk(master, SVC_I3C_QUIRK_FALSE_SLVSTART)) { 683 /* 684 * Re-read MSTATUS to obtain the latest state and avoid 685 * missing an IBI that arrives after MSTATUS is latched 686 * but before SLVSTART is cleared. 687 */ 688 active = readl(master->regs + SVC_I3C_MSTATUS); 689 690 /* Ignore the false event */ 691 if (!SVC_I3C_MSTATUS_STATE_SLVREQ(active)) 692 return IRQ_HANDLED; 693 } 694 695 /* 696 * The SDA line remains low until the request is processed. 697 * Receive the request in the interrupt context to respond promptly 698 * and restore the bus to idle state. 699 */ 700 svc_i3c_master_ibi_isr(master); 701 702 return IRQ_HANDLED; 703 } 704 705 static int svc_i3c_master_set_speed(struct i3c_master_controller *m, 706 enum i3c_open_drain_speed speed) 707 { 708 struct svc_i3c_master *master = to_svc_i3c_master(m); 709 struct i3c_bus *bus = i3c_master_get_bus(&master->base); 710 u32 ppbaud, odbaud, odhpp, mconfig; 711 unsigned long fclk_rate; 712 int ret; 713 714 ret = pm_runtime_resume_and_get(master->dev); 715 if (ret < 0) { 716 dev_err(master->dev, "<%s> Cannot get runtime PM.\n", __func__); 717 return ret; 718 } 719 720 switch (speed) { 721 case I3C_OPEN_DRAIN_SLOW_SPEED: 722 fclk_rate = clk_get_rate(master->fclk); 723 if (!fclk_rate) { 724 ret = -EINVAL; 725 goto rpm_out; 726 } 727 /* 728 * Set 50% duty-cycle I2C speed to I3C OPEN-DRAIN mode, so the first 729 * broadcast address is visible to all I2C/I3C devices on the I3C bus. 730 * I3C device working as a I2C device will turn off its 50ns Spike 731 * Filter to change to I3C mode. 732 */ 733 mconfig = master->mctrl_config; 734 ppbaud = FIELD_GET(GENMASK(11, 8), mconfig); 735 odhpp = 0; 736 odbaud = DIV_ROUND_UP(fclk_rate, bus->scl_rate.i2c * (2 + 2 * ppbaud)) - 1; 737 mconfig &= ~GENMASK(24, 16); 738 mconfig |= SVC_I3C_MCONFIG_ODBAUD(odbaud) | SVC_I3C_MCONFIG_ODHPP(odhpp); 739 writel(mconfig, master->regs + SVC_I3C_MCONFIG); 740 break; 741 case I3C_OPEN_DRAIN_NORMAL_SPEED: 742 writel(master->mctrl_config, master->regs + SVC_I3C_MCONFIG); 743 break; 744 } 745 746 rpm_out: 747 pm_runtime_put_autosuspend(master->dev); 748 749 return ret; 750 } 751 752 static int svc_i3c_master_bus_init(struct i3c_master_controller *m) 753 { 754 struct svc_i3c_master *master = to_svc_i3c_master(m); 755 struct i3c_bus *bus = i3c_master_get_bus(m); 756 struct i3c_device_info info = {}; 757 unsigned long fclk_rate, fclk_period_ns; 758 unsigned long i2c_period_ns, i2c_scl_rate, i3c_scl_rate; 759 unsigned int high_period_ns, od_low_period_ns; 760 u32 ppbaud, pplow, odhpp, odbaud, odstop, i2cbaud, reg; 761 int ret; 762 763 ret = pm_runtime_resume_and_get(master->dev); 764 if (ret < 0) { 765 dev_err(master->dev, 766 "<%s> cannot resume i3c bus master, err: %d\n", 767 __func__, ret); 768 return ret; 769 } 770 771 /* Timings derivation */ 772 fclk_rate = clk_get_rate(master->fclk); 773 if (!fclk_rate) { 774 ret = -EINVAL; 775 goto rpm_out; 776 } 777 778 fclk_period_ns = DIV_ROUND_UP(1000000000, fclk_rate); 779 i2c_period_ns = DIV_ROUND_UP(1000000000, bus->scl_rate.i2c); 780 i2c_scl_rate = bus->scl_rate.i2c; 781 i3c_scl_rate = bus->scl_rate.i3c; 782 783 /* 784 * Using I3C Push-Pull mode, target is 12.5MHz/80ns period. 785 * Simplest configuration is using a 50% duty-cycle of 40ns. 786 */ 787 ppbaud = DIV_ROUND_UP(fclk_rate / 2, i3c_scl_rate) - 1; 788 pplow = 0; 789 790 /* 791 * Using I3C Open-Drain mode, target is 4.17MHz/240ns with a 792 * duty-cycle tuned so that high levels are filtered out by 793 * the 50ns filter (target being 40ns). 794 */ 795 odhpp = 1; 796 high_period_ns = (ppbaud + 1) * fclk_period_ns; 797 odbaud = DIV_ROUND_UP(fclk_rate, SVC_I3C_QUICK_I2C_CLK * (1 + ppbaud)) - 2; 798 od_low_period_ns = (odbaud + 1) * high_period_ns; 799 800 switch (bus->mode) { 801 case I3C_BUS_MODE_PURE: 802 i2cbaud = 0; 803 odstop = 0; 804 break; 805 case I3C_BUS_MODE_MIXED_FAST: 806 /* 807 * Using I2C Fm+ mode, target is 1MHz/1000ns, the difference 808 * between the high and low period does not really matter. 809 */ 810 i2cbaud = DIV_ROUND_UP(i2c_period_ns, od_low_period_ns) - 2; 811 odstop = 1; 812 break; 813 case I3C_BUS_MODE_MIXED_LIMITED: 814 case I3C_BUS_MODE_MIXED_SLOW: 815 /* I3C PP + I3C OP + I2C OP both use i2c clk rate */ 816 if (ppbaud > SVC_I3C_PPBAUD_MAX) { 817 ppbaud = SVC_I3C_PPBAUD_MAX; 818 pplow = DIV_ROUND_UP(fclk_rate, i3c_scl_rate) - (2 + 2 * ppbaud); 819 } 820 821 high_period_ns = (ppbaud + 1) * fclk_period_ns; 822 odhpp = 0; 823 odbaud = DIV_ROUND_UP(fclk_rate, i2c_scl_rate * (2 + 2 * ppbaud)) - 1; 824 825 od_low_period_ns = (odbaud + 1) * high_period_ns; 826 i2cbaud = DIV_ROUND_UP(i2c_period_ns, od_low_period_ns) - 2; 827 odstop = 1; 828 break; 829 default: 830 goto rpm_out; 831 } 832 833 reg = SVC_I3C_MCONFIG_MASTER_EN | 834 SVC_I3C_MCONFIG_DISTO(0) | 835 SVC_I3C_MCONFIG_HKEEP(0) | 836 SVC_I3C_MCONFIG_ODSTOP(odstop) | 837 SVC_I3C_MCONFIG_PPBAUD(ppbaud) | 838 SVC_I3C_MCONFIG_PPLOW(pplow) | 839 SVC_I3C_MCONFIG_ODBAUD(odbaud) | 840 SVC_I3C_MCONFIG_ODHPP(odhpp) | 841 SVC_I3C_MCONFIG_SKEW(0) | 842 SVC_I3C_MCONFIG_I2CBAUD(i2cbaud); 843 writel(reg, master->regs + SVC_I3C_MCONFIG); 844 845 master->mctrl_config = reg; 846 /* Master core's registration */ 847 ret = i3c_master_get_free_addr(m, 0); 848 if (ret < 0) 849 goto rpm_out; 850 851 info.dyn_addr = ret; 852 853 info.hdr_cap = I3C_CCC_HDR_MODE(I3C_HDR_DDR); 854 855 writel(SVC_MDYNADDR_VALID | SVC_MDYNADDR_ADDR(info.dyn_addr), 856 master->regs + SVC_I3C_MDYNADDR); 857 858 ret = i3c_master_set_info(&master->base, &info); 859 if (ret) 860 goto rpm_out; 861 862 rpm_out: 863 pm_runtime_put_autosuspend(master->dev); 864 865 return ret; 866 } 867 868 static void svc_i3c_master_bus_cleanup(struct i3c_master_controller *m) 869 { 870 struct svc_i3c_master *master = to_svc_i3c_master(m); 871 int ret; 872 873 ret = pm_runtime_resume_and_get(master->dev); 874 if (ret < 0) { 875 dev_err(master->dev, "<%s> Cannot get runtime PM.\n", __func__); 876 return; 877 } 878 879 svc_i3c_master_disable_interrupts(master); 880 881 /* Disable master */ 882 writel(0, master->regs + SVC_I3C_MCONFIG); 883 884 pm_runtime_put_autosuspend(master->dev); 885 } 886 887 static int svc_i3c_master_reserve_slot(struct svc_i3c_master *master) 888 { 889 unsigned int slot; 890 891 if (!(master->free_slots & GENMASK(SVC_I3C_MAX_DEVS - 1, 0))) 892 return -ENOSPC; 893 894 slot = ffs(master->free_slots) - 1; 895 896 master->free_slots &= ~BIT(slot); 897 898 return slot; 899 } 900 901 static void svc_i3c_master_release_slot(struct svc_i3c_master *master, 902 unsigned int slot) 903 { 904 master->free_slots |= BIT(slot); 905 } 906 907 static int svc_i3c_master_attach_i3c_dev(struct i3c_dev_desc *dev) 908 { 909 struct i3c_master_controller *m = i3c_dev_get_master(dev); 910 struct svc_i3c_master *master = to_svc_i3c_master(m); 911 struct svc_i3c_i2c_dev_data *data; 912 int slot; 913 914 slot = svc_i3c_master_reserve_slot(master); 915 if (slot < 0) 916 return slot; 917 918 data = kzalloc_obj(*data); 919 if (!data) { 920 svc_i3c_master_release_slot(master, slot); 921 return -ENOMEM; 922 } 923 924 data->ibi = -1; 925 data->index = slot; 926 master->addrs[slot] = dev->info.dyn_addr ? dev->info.dyn_addr : 927 dev->info.static_addr; 928 master->descs[slot] = dev; 929 930 i3c_dev_set_master_data(dev, data); 931 932 return 0; 933 } 934 935 static int svc_i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev, 936 u8 old_dyn_addr) 937 { 938 struct i3c_master_controller *m = i3c_dev_get_master(dev); 939 struct svc_i3c_master *master = to_svc_i3c_master(m); 940 struct svc_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev); 941 942 master->addrs[data->index] = dev->info.dyn_addr ? dev->info.dyn_addr : 943 dev->info.static_addr; 944 945 return 0; 946 } 947 948 static void svc_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev) 949 { 950 struct svc_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev); 951 struct i3c_master_controller *m = i3c_dev_get_master(dev); 952 struct svc_i3c_master *master = to_svc_i3c_master(m); 953 954 master->addrs[data->index] = 0; 955 svc_i3c_master_release_slot(master, data->index); 956 957 kfree(data); 958 } 959 960 static int svc_i3c_master_attach_i2c_dev(struct i2c_dev_desc *dev) 961 { 962 struct i3c_master_controller *m = i2c_dev_get_master(dev); 963 struct svc_i3c_master *master = to_svc_i3c_master(m); 964 struct svc_i3c_i2c_dev_data *data; 965 int slot; 966 967 slot = svc_i3c_master_reserve_slot(master); 968 if (slot < 0) 969 return slot; 970 971 data = kzalloc_obj(*data); 972 if (!data) { 973 svc_i3c_master_release_slot(master, slot); 974 return -ENOMEM; 975 } 976 977 data->index = slot; 978 master->addrs[slot] = dev->addr; 979 980 i2c_dev_set_master_data(dev, data); 981 982 return 0; 983 } 984 985 static void svc_i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev) 986 { 987 struct svc_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev); 988 struct i3c_master_controller *m = i2c_dev_get_master(dev); 989 struct svc_i3c_master *master = to_svc_i3c_master(m); 990 991 svc_i3c_master_release_slot(master, data->index); 992 993 kfree(data); 994 } 995 996 static int svc_i3c_master_readb(struct svc_i3c_master *master, u8 *dst, 997 unsigned int len) 998 { 999 int ret, i; 1000 u32 reg; 1001 1002 for (i = 0; i < len; i++) { 1003 ret = readl_poll_timeout_atomic(master->regs + SVC_I3C_MSTATUS, 1004 reg, 1005 SVC_I3C_MSTATUS_RXPEND(reg), 1006 0, 1000); 1007 if (ret) 1008 return ret; 1009 1010 dst[i] = readl(master->regs + SVC_I3C_MRDATAB); 1011 } 1012 1013 return 0; 1014 } 1015 1016 static int svc_i3c_master_do_daa_locked(struct svc_i3c_master *master, 1017 u8 *addrs, unsigned int *count) 1018 { 1019 u64 prov_id[SVC_I3C_MAX_DEVS] = {}, nacking_prov_id = 0; 1020 unsigned int dev_nb = 0, last_addr = 0, dyn_addr = 0; 1021 u32 reg; 1022 int ret, i; 1023 1024 svc_i3c_master_flush_fifo(master); 1025 1026 while (true) { 1027 /* clean SVC_I3C_MINT_IBIWON w1c bits */ 1028 writel(SVC_I3C_MINT_IBIWON, master->regs + SVC_I3C_MSTATUS); 1029 1030 /* SVC_I3C_MCTRL_REQUEST_PROC_DAA have two mode, ENTER DAA or PROCESS DAA. 1031 * 1032 * ENTER DAA: 1033 * 1 will issue START, 7E, ENTDAA, and then emits 7E/R to process first target. 1034 * 2 Stops just before the new Dynamic Address (DA) is to be emitted. 1035 * 1036 * PROCESS DAA: 1037 * 1 The DA is written using MWDATAB or ADDR bits 6:0. 1038 * 2 ProcessDAA is requested again to write the new address, and then starts the 1039 * next (START, 7E, ENTDAA) unless marked to STOP; an MSTATUS indicating NACK 1040 * means DA was not accepted (e.g. parity error). If PROCESSDAA is NACKed on the 1041 * 7E/R, which means no more Slaves need a DA, then a COMPLETE will be signaled 1042 * (along with DONE), and a STOP issued automatically. 1043 */ 1044 writel(SVC_I3C_MCTRL_REQUEST_PROC_DAA | 1045 SVC_I3C_MCTRL_TYPE_I3C | 1046 SVC_I3C_MCTRL_IBIRESP_NACK | 1047 SVC_I3C_MCTRL_DIR(SVC_I3C_MCTRL_DIR_WRITE), 1048 master->regs + SVC_I3C_MCTRL); 1049 1050 /* 1051 * Either one slave will send its ID, or the assignment process 1052 * is done. 1053 */ 1054 ret = readl_poll_timeout_atomic(master->regs + SVC_I3C_MSTATUS, 1055 reg, 1056 SVC_I3C_MSTATUS_RXPEND(reg) | 1057 SVC_I3C_MSTATUS_MCTRLDONE(reg), 1058 1, 1000); 1059 if (ret) 1060 break; 1061 1062 if (SVC_I3C_MSTATUS_RXPEND(reg)) { 1063 u8 data[6]; 1064 1065 /* 1066 * One slave sends its ID to request for address assignment, 1067 * prefilling the dynamic address can reduce SCL clock stalls 1068 * and also fix the SVC_I3C_QUIRK_FIFO_EMPTY quirk. 1069 * 1070 * Ideally, prefilling before the processDAA command is better. 1071 * However, it requires an additional check to write the dyn_addr 1072 * at the right time because the driver needs to write the processDAA 1073 * command twice for one assignment. 1074 * Prefilling here is safe and efficient because the FIFO starts 1075 * filling within a few hundred nanoseconds, which is significantly 1076 * faster compared to the 64 SCL clock cycles. 1077 */ 1078 ret = i3c_master_get_free_addr(&master->base, last_addr + 1); 1079 if (ret < 0) 1080 break; 1081 1082 dyn_addr = ret; 1083 writel(dyn_addr, master->regs + SVC_I3C_MWDATAB); 1084 1085 /* 1086 * We only care about the 48-bit provisioned ID yet to 1087 * be sure a device does not nack an address twice. 1088 * Otherwise, we would just need to flush the RX FIFO. 1089 */ 1090 ret = svc_i3c_master_readb(master, data, 6); 1091 if (ret) 1092 break; 1093 1094 for (i = 0; i < 6; i++) 1095 prov_id[dev_nb] |= (u64)(data[i]) << (8 * (5 - i)); 1096 1097 /* We do not care about the BCR and DCR yet */ 1098 ret = svc_i3c_master_readb(master, data, 2); 1099 if (ret) 1100 break; 1101 } else if (SVC_I3C_MSTATUS_IBIWON(reg)) { 1102 ret = svc_i3c_master_handle_ibi_won(master, reg); 1103 if (ret) 1104 break; 1105 continue; 1106 } else if (SVC_I3C_MSTATUS_MCTRLDONE(reg)) { 1107 if (SVC_I3C_MSTATUS_STATE_IDLE(reg) && 1108 SVC_I3C_MSTATUS_COMPLETE(reg)) { 1109 /* 1110 * All devices received and acked they dynamic 1111 * address, this is the natural end of the DAA 1112 * procedure. 1113 * 1114 * Hardware will auto emit STOP at this case. 1115 */ 1116 *count = dev_nb; 1117 return 0; 1118 1119 } else if (SVC_I3C_MSTATUS_NACKED(reg)) { 1120 /* No I3C devices attached */ 1121 if (dev_nb == 0) { 1122 /* 1123 * Hardware can't treat first NACK for ENTAA as normal 1124 * COMPLETE. So need manual emit STOP. 1125 */ 1126 ret = 0; 1127 *count = 0; 1128 break; 1129 } 1130 1131 /* 1132 * A slave device nacked the address, this is 1133 * allowed only once, DAA will be stopped and 1134 * then resumed. The same device is supposed to 1135 * answer again immediately and shall ack the 1136 * address this time. 1137 */ 1138 if (prov_id[dev_nb] == nacking_prov_id) { 1139 ret = -EIO; 1140 break; 1141 } 1142 1143 dev_nb--; 1144 nacking_prov_id = prov_id[dev_nb]; 1145 svc_i3c_master_emit_stop(master); 1146 1147 continue; 1148 } else { 1149 break; 1150 } 1151 } 1152 1153 /* Wait for the slave to be ready to receive its address */ 1154 ret = readl_poll_timeout_atomic(master->regs + SVC_I3C_MSTATUS, 1155 reg, 1156 SVC_I3C_MSTATUS_MCTRLDONE(reg) && 1157 SVC_I3C_MSTATUS_STATE_DAA(reg) && 1158 SVC_I3C_MSTATUS_BETWEEN(reg), 1159 0, 1000); 1160 if (ret) 1161 break; 1162 1163 addrs[dev_nb] = dyn_addr; 1164 dev_dbg(master->dev, "DAA: device %d assigned to 0x%02x\n", 1165 dev_nb, addrs[dev_nb]); 1166 last_addr = addrs[dev_nb++]; 1167 } 1168 1169 /* Need manual issue STOP except for Complete condition */ 1170 svc_i3c_master_emit_stop(master); 1171 svc_i3c_master_flush_fifo(master); 1172 1173 return ret; 1174 } 1175 1176 static int svc_i3c_update_ibirules(struct svc_i3c_master *master) 1177 { 1178 struct i3c_dev_desc *dev; 1179 u32 reg_mbyte = 0, reg_nobyte = SVC_I3C_IBIRULES_NOBYTE; 1180 unsigned int mbyte_addr_ok = 0, mbyte_addr_ko = 0, nobyte_addr_ok = 0, 1181 nobyte_addr_ko = 0; 1182 bool list_mbyte = false, list_nobyte = false; 1183 1184 /* Create the IBIRULES register for both cases */ 1185 i3c_bus_for_each_i3cdev(&master->base.bus, dev) { 1186 if (!(dev->info.bcr & I3C_BCR_IBI_REQ_CAP)) 1187 continue; 1188 1189 if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD) { 1190 reg_mbyte |= SVC_I3C_IBIRULES_ADDR(mbyte_addr_ok, 1191 dev->info.dyn_addr); 1192 1193 /* IBI rules cannot be applied to devices with MSb=1 */ 1194 if (dev->info.dyn_addr & BIT(7)) 1195 mbyte_addr_ko++; 1196 else 1197 mbyte_addr_ok++; 1198 } else { 1199 reg_nobyte |= SVC_I3C_IBIRULES_ADDR(nobyte_addr_ok, 1200 dev->info.dyn_addr); 1201 1202 /* IBI rules cannot be applied to devices with MSb=1 */ 1203 if (dev->info.dyn_addr & BIT(7)) 1204 nobyte_addr_ko++; 1205 else 1206 nobyte_addr_ok++; 1207 } 1208 } 1209 1210 /* Device list cannot be handled by hardware */ 1211 if (!mbyte_addr_ko && mbyte_addr_ok <= SVC_I3C_IBIRULES_ADDRS) 1212 list_mbyte = true; 1213 1214 if (!nobyte_addr_ko && nobyte_addr_ok <= SVC_I3C_IBIRULES_ADDRS) 1215 list_nobyte = true; 1216 1217 /* No list can be properly handled, return an error */ 1218 if (!list_mbyte && !list_nobyte) 1219 return -ERANGE; 1220 1221 /* Pick the first list that can be handled by hardware, randomly */ 1222 if (list_mbyte) 1223 writel(reg_mbyte, master->regs + SVC_I3C_IBIRULES); 1224 else 1225 writel(reg_nobyte, master->regs + SVC_I3C_IBIRULES); 1226 1227 return 0; 1228 } 1229 1230 static int svc_i3c_master_do_daa(struct i3c_master_controller *m) 1231 { 1232 struct svc_i3c_master *master = to_svc_i3c_master(m); 1233 u8 addrs[SVC_I3C_MAX_DEVS]; 1234 unsigned long flags; 1235 unsigned int dev_nb; 1236 int ret, i; 1237 1238 ret = pm_runtime_resume_and_get(master->dev); 1239 if (ret < 0) { 1240 dev_err(master->dev, "<%s> Cannot get runtime PM.\n", __func__); 1241 return ret; 1242 } 1243 1244 spin_lock_irqsave(&master->xferqueue.lock, flags); 1245 1246 if (svc_has_daa_corrupt(master)) 1247 writel(master->mctrl_config | SVC_I3C_MCONFIG_SKEW(1), 1248 master->regs + SVC_I3C_MCONFIG); 1249 1250 ret = svc_i3c_master_do_daa_locked(master, addrs, &dev_nb); 1251 1252 if (svc_has_daa_corrupt(master)) 1253 writel(master->mctrl_config, master->regs + SVC_I3C_MCONFIG); 1254 1255 spin_unlock_irqrestore(&master->xferqueue.lock, flags); 1256 1257 svc_i3c_master_clear_merrwarn(master); 1258 if (ret) 1259 goto rpm_out; 1260 1261 /* 1262 * Register all devices who participated to the core 1263 * 1264 * If two devices (A and B) are detected in DAA and address 0xa is assigned to 1265 * device A and 0xb to device B, a failure in i3c_master_add_i3c_dev_locked() 1266 * for device A (addr: 0xa) could prevent device B (addr: 0xb) from being 1267 * registered on the bus. The I3C stack might still consider 0xb a free 1268 * address. If a subsequent Hotjoin occurs, 0xb might be assigned to Device A, 1269 * causing both devices A and B to use the same address 0xb, violating the I3C 1270 * specification. 1271 * 1272 * The return value for i3c_master_add_i3c_dev_locked() should not be checked 1273 * because subsequent steps will scan the entire I3C bus, independent of 1274 * whether i3c_master_add_i3c_dev_locked() returns success. 1275 * 1276 * If device A registration fails, there is still a chance to register device 1277 * B. i3c_master_add_i3c_dev_locked() can reset DAA if a failure occurs while 1278 * retrieving device information. 1279 */ 1280 for (i = 0; i < dev_nb; i++) 1281 i3c_master_add_i3c_dev_locked(m, addrs[i]); 1282 1283 /* Configure IBI auto-rules */ 1284 ret = svc_i3c_update_ibirules(master); 1285 if (ret) 1286 dev_err(master->dev, "Cannot handle such a list of devices\n"); 1287 1288 rpm_out: 1289 pm_runtime_put_autosuspend(master->dev); 1290 1291 return ret; 1292 } 1293 1294 static int svc_i3c_master_read(struct svc_i3c_master *master, 1295 u8 *in, unsigned int len) 1296 { 1297 int offset = 0, i; 1298 u32 mdctrl, mstatus; 1299 bool completed = false; 1300 unsigned int count; 1301 unsigned long start = jiffies; 1302 1303 while (!completed) { 1304 mstatus = readl(master->regs + SVC_I3C_MSTATUS); 1305 if (SVC_I3C_MSTATUS_COMPLETE(mstatus) != 0) 1306 completed = true; 1307 1308 if (time_after(jiffies, start + msecs_to_jiffies(1000))) { 1309 dev_dbg(master->dev, "I3C read timeout\n"); 1310 return -ETIMEDOUT; 1311 } 1312 1313 mdctrl = readl(master->regs + SVC_I3C_MDATACTRL); 1314 count = SVC_I3C_MDATACTRL_RXCOUNT(mdctrl); 1315 if (offset + count > len) { 1316 dev_err(master->dev, "I3C receive length too long!\n"); 1317 return -EINVAL; 1318 } 1319 for (i = 0; i < count; i++) 1320 in[offset + i] = readl(master->regs + SVC_I3C_MRDATAB); 1321 1322 offset += count; 1323 } 1324 1325 return offset; 1326 } 1327 1328 static int svc_i3c_master_write(struct svc_i3c_master *master, 1329 const u8 *out, unsigned int len) 1330 { 1331 int offset = 0, ret; 1332 u32 mdctrl; 1333 1334 while (offset < len) { 1335 ret = readl_poll_timeout(master->regs + SVC_I3C_MDATACTRL, 1336 mdctrl, 1337 !(mdctrl & SVC_I3C_MDATACTRL_TXFULL), 1338 0, 1000); 1339 if (ret) 1340 return ret; 1341 1342 /* 1343 * The last byte to be sent over the bus must either have the 1344 * "end" bit set or be written in MWDATABE. 1345 */ 1346 if (likely(offset < (len - 1))) 1347 writel(out[offset++], master->regs + SVC_I3C_MWDATAB); 1348 else 1349 writel(out[offset++], master->regs + SVC_I3C_MWDATABE); 1350 } 1351 1352 return 0; 1353 } 1354 1355 static int svc_i3c_master_xfer(struct svc_i3c_master *master, 1356 u32 rnw_cmd, unsigned int xfer_type, u8 addr, 1357 u8 *in, const u8 *out, unsigned int xfer_len, 1358 unsigned int *actual_len, bool continued, bool repeat_start) 1359 { 1360 bool rnw = svc_cmd_is_read(rnw_cmd, xfer_type); 1361 int retry = repeat_start ? 1 : 2; 1362 u32 reg; 1363 int ret; 1364 1365 /* clean SVC_I3C_MINT_IBIWON w1c bits */ 1366 writel(SVC_I3C_MINT_IBIWON, master->regs + SVC_I3C_MSTATUS); 1367 1368 if (xfer_type == SVC_I3C_MCTRL_TYPE_DDR) { 1369 /* DDR command need prefill into FIFO */ 1370 writel(rnw_cmd, master->regs + SVC_I3C_MWDATAB); 1371 if (!rnw) { 1372 /* write data also need prefill into FIFO */ 1373 ret = svc_i3c_master_write(master, out, xfer_len); 1374 if (ret) 1375 goto emit_stop; 1376 } 1377 } 1378 1379 while (retry--) { 1380 writel(SVC_I3C_MCTRL_REQUEST_START_ADDR | 1381 xfer_type | 1382 SVC_I3C_MCTRL_IBIRESP_NACK | 1383 SVC_I3C_MCTRL_DIR(rnw) | 1384 SVC_I3C_MCTRL_ADDR(addr) | 1385 SVC_I3C_MCTRL_RDTERM(*actual_len), 1386 master->regs + SVC_I3C_MCTRL); 1387 1388 /* 1389 * The entire transaction can consist of multiple write transfers. 1390 * Prefilling before EmitStartAddr causes the data to be emitted 1391 * immediately, becoming part of the previous transfer. 1392 * The only way to work around this hardware issue is to let the 1393 * FIFO start filling as soon as possible after EmitStartAddr. 1394 */ 1395 if (svc_has_quirk(master, SVC_I3C_QUIRK_FIFO_EMPTY) && !rnw && xfer_len) { 1396 u32 space, end, len; 1397 1398 reg = readl(master->regs + SVC_I3C_MDATACTRL); 1399 space = SVC_I3C_FIFO_SIZE - SVC_I3C_MDATACTRL_TXCOUNT(reg); 1400 if (space) { 1401 end = xfer_len > space ? 0 : SVC_I3C_MWDATAB_END; 1402 len = min_t(u32, xfer_len, space); 1403 writesb(master->regs + SVC_I3C_MWDATAB1, out, len - 1); 1404 /* Mark END bit if this is the last byte */ 1405 writel(out[len - 1] | end, master->regs + SVC_I3C_MWDATAB); 1406 xfer_len -= len; 1407 out += len; 1408 } 1409 } 1410 1411 ret = readl_poll_timeout(master->regs + SVC_I3C_MSTATUS, reg, 1412 SVC_I3C_MSTATUS_MCTRLDONE(reg), 0, 1000); 1413 if (ret) 1414 goto emit_stop; 1415 1416 /* 1417 * According to I3C spec ver 1.1.1, 5.1.2.2.3 Consequence of Controller Starting a 1418 * Frame with I3C Target Address. 1419 * 1420 * The I3C Controller normally should start a Frame, the Address may be arbitrated, 1421 * and so the Controller shall monitor to see whether an In-Band Interrupt request, 1422 * a Controller Role Request (i.e., Secondary Controller requests to become the 1423 * Active Controller), or a Hot-Join Request has been made. 1424 * 1425 * If missed IBIWON check, the wrong data will be return. When IBIWON happen, issue 1426 * repeat start. Address arbitrate only happen at START, never happen at REPEAT 1427 * start. 1428 */ 1429 if (SVC_I3C_MSTATUS_IBIWON(reg)) { 1430 ret = svc_i3c_master_handle_ibi_won(master, reg); 1431 if (ret) 1432 goto emit_stop; 1433 continue; 1434 } 1435 1436 if (readl(master->regs + SVC_I3C_MERRWARN) & SVC_I3C_MERRWARN_NACK) { 1437 /* 1438 * According to I3C Spec 1.1.1, 11-Jun-2021, section: 5.1.2.2.3. 1439 * If the Controller chooses to start an I3C Message with an I3C Dynamic 1440 * Address, then special provisions shall be made because that same I3C 1441 * Target may be initiating an IBI or a Controller Role Request. So, one of 1442 * three things may happen: (skip 1, 2) 1443 * 1444 * 3. The Addresses match and the RnW bits also match, and so neither 1445 * Controller nor Target will ACK since both are expecting the other side to 1446 * provide ACK. As a result, each side might think it had "won" arbitration, 1447 * but neither side would continue, as each would subsequently see that the 1448 * other did not provide ACK. 1449 * ... 1450 * For either value of RnW: Due to the NACK, the Controller shall defer the 1451 * Private Write or Private Read, and should typically transmit the Target 1452 * Address again after a Repeated START (i.e., the next one or any one prior 1453 * to a STOP in the Frame). Since the Address Header following a Repeated 1454 * START is not arbitrated, the Controller will always win (see Section 1455 * 5.1.2.2.4). 1456 */ 1457 if (retry && addr != 0x7e) { 1458 writel(SVC_I3C_MERRWARN_NACK, master->regs + SVC_I3C_MERRWARN); 1459 } else { 1460 ret = -ENXIO; 1461 *actual_len = 0; 1462 goto emit_stop; 1463 } 1464 } else { 1465 break; 1466 } 1467 } 1468 1469 if (rnw) 1470 ret = svc_i3c_master_read(master, in, xfer_len); 1471 else if (xfer_type != SVC_I3C_MCTRL_TYPE_DDR) 1472 ret = svc_i3c_master_write(master, out, xfer_len); 1473 if (ret < 0) 1474 goto emit_stop; 1475 1476 if (rnw) 1477 *actual_len = ret; 1478 1479 ret = readl_poll_timeout(master->regs + SVC_I3C_MSTATUS, reg, 1480 SVC_I3C_MSTATUS_COMPLETE(reg), 0, 1000); 1481 if (ret) 1482 goto emit_stop; 1483 1484 if (xfer_type == SVC_I3C_MCTRL_TYPE_DDR && 1485 (readl(master->regs + SVC_I3C_MERRWARN) & SVC_I3C_MERRWARN_CRC)) { 1486 ret = -ENXIO; 1487 goto emit_stop; 1488 } 1489 1490 writel(SVC_I3C_MINT_COMPLETE, master->regs + SVC_I3C_MSTATUS); 1491 1492 if (!continued) { 1493 if (xfer_type != SVC_I3C_MCTRL_TYPE_DDR) 1494 svc_i3c_master_emit_stop(master); 1495 else 1496 svc_i3c_master_emit_force_exit(master); 1497 1498 /* Wait idle if stop is sent. */ 1499 ret = readl_poll_timeout(master->regs + SVC_I3C_MSTATUS, reg, 1500 SVC_I3C_MSTATUS_STATE_IDLE(reg), 1501 0, 1000); 1502 if (ret) 1503 goto cleanup; 1504 } 1505 1506 return 0; 1507 1508 emit_stop: 1509 if (xfer_type != SVC_I3C_MCTRL_TYPE_DDR) 1510 svc_i3c_master_emit_stop(master); 1511 else 1512 svc_i3c_master_emit_force_exit(master); 1513 1514 cleanup: 1515 svc_i3c_master_clear_merrwarn(master); 1516 svc_i3c_master_flush_fifo(master); 1517 1518 return ret; 1519 } 1520 1521 static struct svc_i3c_xfer * 1522 svc_i3c_master_alloc_xfer(struct svc_i3c_master *master, unsigned int ncmds) 1523 { 1524 struct svc_i3c_xfer *xfer; 1525 1526 xfer = kzalloc_flex(*xfer, cmds, ncmds); 1527 if (!xfer) 1528 return NULL; 1529 1530 INIT_LIST_HEAD(&xfer->node); 1531 xfer->ncmds = ncmds; 1532 xfer->ret = -ETIMEDOUT; 1533 1534 return xfer; 1535 } 1536 1537 static void svc_i3c_master_free_xfer(struct svc_i3c_xfer *xfer) 1538 { 1539 kfree(xfer); 1540 } 1541 1542 static void svc_i3c_master_dequeue_xfer_locked(struct svc_i3c_master *master, 1543 struct svc_i3c_xfer *xfer) 1544 { 1545 if (master->xferqueue.cur == xfer) 1546 master->xferqueue.cur = NULL; 1547 else 1548 list_del_init(&xfer->node); 1549 } 1550 1551 static void svc_i3c_master_dequeue_xfer(struct svc_i3c_master *master, 1552 struct svc_i3c_xfer *xfer) 1553 { 1554 unsigned long flags; 1555 1556 spin_lock_irqsave(&master->xferqueue.lock, flags); 1557 svc_i3c_master_dequeue_xfer_locked(master, xfer); 1558 spin_unlock_irqrestore(&master->xferqueue.lock, flags); 1559 } 1560 1561 static int i3c_mode_to_svc_type(enum i3c_xfer_mode mode) 1562 { 1563 return (mode == I3C_SDR) ? SVC_I3C_MCTRL_TYPE_I3C : SVC_I3C_MCTRL_TYPE_DDR; 1564 } 1565 1566 static void svc_i3c_master_start_xfer_locked(struct svc_i3c_master *master) 1567 { 1568 struct svc_i3c_xfer *xfer = master->xferqueue.cur; 1569 int ret, i; 1570 1571 if (!xfer) 1572 return; 1573 1574 svc_i3c_master_clear_merrwarn(master); 1575 svc_i3c_master_flush_fifo(master); 1576 1577 for (i = 0; i < xfer->ncmds; i++) { 1578 struct svc_i3c_cmd *cmd = &xfer->cmds[i]; 1579 1580 ret = svc_i3c_master_xfer(master, cmd->rnw_cmd, xfer->type, 1581 cmd->addr, cmd->in, cmd->out, 1582 cmd->len, &cmd->actual_len, 1583 cmd->continued, i > 0); 1584 /* cmd->xfer is NULL if I2C or CCC transfer */ 1585 if (cmd->xfer) 1586 cmd->xfer->actual_len = cmd->actual_len; 1587 1588 if (ret) 1589 break; 1590 } 1591 1592 xfer->ret = ret; 1593 complete(&xfer->comp); 1594 1595 if (ret < 0) 1596 svc_i3c_master_dequeue_xfer_locked(master, xfer); 1597 1598 xfer = list_first_entry_or_null(&master->xferqueue.list, 1599 struct svc_i3c_xfer, 1600 node); 1601 if (xfer) 1602 list_del_init(&xfer->node); 1603 1604 master->xferqueue.cur = xfer; 1605 svc_i3c_master_start_xfer_locked(master); 1606 } 1607 1608 static void svc_i3c_master_enqueue_xfer(struct svc_i3c_master *master, 1609 struct svc_i3c_xfer *xfer) 1610 { 1611 unsigned long flags; 1612 int ret; 1613 1614 ret = pm_runtime_resume_and_get(master->dev); 1615 if (ret < 0) { 1616 dev_err(master->dev, "<%s> Cannot get runtime PM.\n", __func__); 1617 return; 1618 } 1619 1620 init_completion(&xfer->comp); 1621 spin_lock_irqsave(&master->xferqueue.lock, flags); 1622 if (master->xferqueue.cur) { 1623 list_add_tail(&xfer->node, &master->xferqueue.list); 1624 } else { 1625 master->xferqueue.cur = xfer; 1626 svc_i3c_master_start_xfer_locked(master); 1627 } 1628 spin_unlock_irqrestore(&master->xferqueue.lock, flags); 1629 1630 pm_runtime_put_autosuspend(master->dev); 1631 } 1632 1633 static bool 1634 svc_i3c_master_supports_ccc_cmd(struct i3c_master_controller *master, 1635 const struct i3c_ccc_cmd *cmd) 1636 { 1637 /* No software support for CCC commands targeting more than one slave */ 1638 return (cmd->ndests == 1); 1639 } 1640 1641 static int svc_i3c_master_send_bdcast_ccc_cmd(struct svc_i3c_master *master, 1642 struct i3c_ccc_cmd *ccc) 1643 { 1644 unsigned int xfer_len = ccc->dests[0].payload.len + 1; 1645 struct svc_i3c_xfer *xfer; 1646 struct svc_i3c_cmd *cmd; 1647 u8 *buf; 1648 int ret; 1649 1650 xfer = svc_i3c_master_alloc_xfer(master, 1); 1651 if (!xfer) 1652 return -ENOMEM; 1653 1654 buf = kmalloc(xfer_len, GFP_KERNEL); 1655 if (!buf) { 1656 svc_i3c_master_free_xfer(xfer); 1657 return -ENOMEM; 1658 } 1659 1660 buf[0] = ccc->id; 1661 memcpy(&buf[1], ccc->dests[0].payload.data, ccc->dests[0].payload.len); 1662 1663 xfer->type = SVC_I3C_MCTRL_TYPE_I3C; 1664 1665 cmd = &xfer->cmds[0]; 1666 cmd->addr = ccc->dests[0].addr; 1667 cmd->rnw = ccc->rnw; 1668 cmd->in = NULL; 1669 cmd->out = buf; 1670 cmd->len = xfer_len; 1671 cmd->actual_len = 0; 1672 cmd->continued = false; 1673 1674 mutex_lock(&master->lock); 1675 svc_i3c_master_enqueue_xfer(master, xfer); 1676 if (!wait_for_completion_timeout(&xfer->comp, msecs_to_jiffies(1000))) 1677 svc_i3c_master_dequeue_xfer(master, xfer); 1678 mutex_unlock(&master->lock); 1679 1680 ret = xfer->ret; 1681 kfree(buf); 1682 svc_i3c_master_free_xfer(xfer); 1683 1684 return ret; 1685 } 1686 1687 static int svc_i3c_master_send_direct_ccc_cmd(struct svc_i3c_master *master, 1688 struct i3c_ccc_cmd *ccc) 1689 { 1690 unsigned int xfer_len = ccc->dests[0].payload.len; 1691 unsigned int actual_len = ccc->rnw ? xfer_len : 0; 1692 struct svc_i3c_xfer *xfer; 1693 struct svc_i3c_cmd *cmd; 1694 int ret; 1695 1696 xfer = svc_i3c_master_alloc_xfer(master, 2); 1697 if (!xfer) 1698 return -ENOMEM; 1699 1700 xfer->type = SVC_I3C_MCTRL_TYPE_I3C; 1701 1702 /* Broadcasted message */ 1703 cmd = &xfer->cmds[0]; 1704 cmd->addr = I3C_BROADCAST_ADDR; 1705 cmd->rnw = 0; 1706 cmd->in = NULL; 1707 cmd->out = &ccc->id; 1708 cmd->len = 1; 1709 cmd->actual_len = 0; 1710 cmd->continued = true; 1711 1712 /* Directed message */ 1713 cmd = &xfer->cmds[1]; 1714 cmd->addr = ccc->dests[0].addr; 1715 cmd->rnw = ccc->rnw; 1716 cmd->in = ccc->rnw ? ccc->dests[0].payload.data : NULL; 1717 cmd->out = ccc->rnw ? NULL : ccc->dests[0].payload.data; 1718 cmd->len = xfer_len; 1719 cmd->actual_len = actual_len; 1720 cmd->continued = false; 1721 1722 mutex_lock(&master->lock); 1723 svc_i3c_master_enqueue_xfer(master, xfer); 1724 if (!wait_for_completion_timeout(&xfer->comp, msecs_to_jiffies(1000))) 1725 svc_i3c_master_dequeue_xfer(master, xfer); 1726 mutex_unlock(&master->lock); 1727 1728 if (ccc->rnw) 1729 ccc->dests[0].payload.actual_len = cmd->actual_len; 1730 1731 ret = xfer->ret; 1732 svc_i3c_master_free_xfer(xfer); 1733 1734 return ret; 1735 } 1736 1737 static int svc_i3c_master_send_ccc_cmd(struct i3c_master_controller *m, 1738 struct i3c_ccc_cmd *cmd) 1739 { 1740 struct svc_i3c_master *master = to_svc_i3c_master(m); 1741 bool broadcast = cmd->id < 0x80; 1742 int ret; 1743 1744 if (broadcast) 1745 ret = svc_i3c_master_send_bdcast_ccc_cmd(master, cmd); 1746 else 1747 ret = svc_i3c_master_send_direct_ccc_cmd(master, cmd); 1748 1749 if (ret) 1750 cmd->err = I3C_ERROR_M2; 1751 1752 return ret; 1753 } 1754 1755 static int svc_i3c_master_i3c_xfers(struct i3c_dev_desc *dev, struct i3c_xfer *xfers, 1756 int nxfers, enum i3c_xfer_mode mode) 1757 { 1758 struct i3c_master_controller *m = i3c_dev_get_master(dev); 1759 struct svc_i3c_master *master = to_svc_i3c_master(m); 1760 struct svc_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev); 1761 struct svc_i3c_xfer *xfer; 1762 int ret, i; 1763 1764 if (mode != I3C_SDR) { 1765 /* 1766 * Only support data size less than FIFO SIZE when using DDR 1767 * mode. First entry is cmd in FIFO, so actual available FIFO 1768 * for data is SVC_I3C_FIFO_SIZE - 2 since DDR only supports 1769 * even length. 1770 */ 1771 for (i = 0; i < nxfers; i++) 1772 if (xfers[i].len > SVC_I3C_FIFO_SIZE - 2) 1773 return -EINVAL; 1774 } 1775 1776 xfer = svc_i3c_master_alloc_xfer(master, nxfers); 1777 if (!xfer) 1778 return -ENOMEM; 1779 1780 xfer->type = i3c_mode_to_svc_type(mode); 1781 1782 for (i = 0; i < nxfers; i++) { 1783 u32 rnw_cmd = (mode == I3C_SDR) ? xfers[i].rnw : xfers[i].cmd; 1784 bool rnw = svc_cmd_is_read(rnw_cmd, xfer->type); 1785 struct svc_i3c_cmd *cmd = &xfer->cmds[i]; 1786 1787 cmd->xfer = &xfers[i]; 1788 cmd->addr = master->addrs[data->index]; 1789 cmd->rnw_cmd = rnw_cmd; 1790 cmd->in = rnw ? xfers[i].data.in : NULL; 1791 cmd->out = rnw ? NULL : xfers[i].data.out; 1792 cmd->len = xfers[i].len; 1793 cmd->actual_len = rnw ? xfers[i].len : 0; 1794 cmd->continued = (i + 1) < nxfers; 1795 } 1796 1797 mutex_lock(&master->lock); 1798 svc_i3c_master_enqueue_xfer(master, xfer); 1799 if (!wait_for_completion_timeout(&xfer->comp, msecs_to_jiffies(1000))) 1800 svc_i3c_master_dequeue_xfer(master, xfer); 1801 mutex_unlock(&master->lock); 1802 1803 ret = xfer->ret; 1804 svc_i3c_master_free_xfer(xfer); 1805 1806 return ret; 1807 } 1808 1809 static int svc_i3c_master_i2c_xfers(struct i2c_dev_desc *dev, 1810 struct i2c_msg *xfers, 1811 int nxfers) 1812 { 1813 struct i3c_master_controller *m = i2c_dev_get_master(dev); 1814 struct svc_i3c_master *master = to_svc_i3c_master(m); 1815 struct svc_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev); 1816 struct svc_i3c_xfer *xfer; 1817 int ret, i; 1818 1819 xfer = svc_i3c_master_alloc_xfer(master, nxfers); 1820 if (!xfer) 1821 return -ENOMEM; 1822 1823 xfer->type = SVC_I3C_MCTRL_TYPE_I2C; 1824 1825 for (i = 0; i < nxfers; i++) { 1826 struct svc_i3c_cmd *cmd = &xfer->cmds[i]; 1827 1828 cmd->addr = master->addrs[data->index]; 1829 cmd->rnw = xfers[i].flags & I2C_M_RD; 1830 cmd->in = cmd->rnw ? xfers[i].buf : NULL; 1831 cmd->out = cmd->rnw ? NULL : xfers[i].buf; 1832 cmd->len = xfers[i].len; 1833 cmd->actual_len = cmd->rnw ? xfers[i].len : 0; 1834 cmd->continued = (i + 1 < nxfers); 1835 } 1836 1837 mutex_lock(&master->lock); 1838 svc_i3c_master_enqueue_xfer(master, xfer); 1839 if (!wait_for_completion_timeout(&xfer->comp, m->i2c.timeout)) 1840 svc_i3c_master_dequeue_xfer(master, xfer); 1841 mutex_unlock(&master->lock); 1842 1843 ret = xfer->ret; 1844 svc_i3c_master_free_xfer(xfer); 1845 1846 return ret; 1847 } 1848 1849 static int svc_i3c_master_request_ibi(struct i3c_dev_desc *dev, 1850 const struct i3c_ibi_setup *req) 1851 { 1852 struct i3c_master_controller *m = i3c_dev_get_master(dev); 1853 struct svc_i3c_master *master = to_svc_i3c_master(m); 1854 struct svc_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev); 1855 unsigned long flags; 1856 unsigned int i; 1857 1858 if (dev->ibi->max_payload_len > SVC_I3C_FIFO_SIZE) { 1859 dev_err(master->dev, "IBI max payload %d should be < %d\n", 1860 dev->ibi->max_payload_len, SVC_I3C_FIFO_SIZE); 1861 return -ERANGE; 1862 } 1863 1864 data->ibi_pool = i3c_generic_ibi_alloc_pool(dev, req); 1865 if (IS_ERR(data->ibi_pool)) 1866 return PTR_ERR(data->ibi_pool); 1867 1868 spin_lock_irqsave(&master->ibi.lock, flags); 1869 for (i = 0; i < master->ibi.num_slots; i++) { 1870 if (!master->ibi.slots[i]) { 1871 data->ibi = i; 1872 master->ibi.slots[i] = dev; 1873 break; 1874 } 1875 } 1876 spin_unlock_irqrestore(&master->ibi.lock, flags); 1877 1878 if (i < master->ibi.num_slots) 1879 return 0; 1880 1881 i3c_generic_ibi_free_pool(data->ibi_pool); 1882 data->ibi_pool = NULL; 1883 1884 return -ENOSPC; 1885 } 1886 1887 static void svc_i3c_master_free_ibi(struct i3c_dev_desc *dev) 1888 { 1889 struct i3c_master_controller *m = i3c_dev_get_master(dev); 1890 struct svc_i3c_master *master = to_svc_i3c_master(m); 1891 struct svc_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev); 1892 unsigned long flags; 1893 1894 spin_lock_irqsave(&master->ibi.lock, flags); 1895 master->ibi.slots[data->ibi] = NULL; 1896 data->ibi = -1; 1897 spin_unlock_irqrestore(&master->ibi.lock, flags); 1898 1899 i3c_generic_ibi_free_pool(data->ibi_pool); 1900 } 1901 1902 static int svc_i3c_master_enable_ibi(struct i3c_dev_desc *dev) 1903 { 1904 struct i3c_master_controller *m = i3c_dev_get_master(dev); 1905 struct svc_i3c_master *master = to_svc_i3c_master(m); 1906 int ret; 1907 1908 ret = pm_runtime_resume_and_get(master->dev); 1909 if (ret < 0) { 1910 dev_err(master->dev, "<%s> Cannot get runtime PM.\n", __func__); 1911 return ret; 1912 } 1913 1914 master->enabled_events++; 1915 svc_i3c_master_enable_interrupts(master, SVC_I3C_MINT_SLVSTART); 1916 1917 return i3c_master_enec_locked(m, dev->info.dyn_addr, I3C_CCC_EVENT_SIR); 1918 } 1919 1920 static int svc_i3c_master_disable_ibi(struct i3c_dev_desc *dev) 1921 { 1922 struct i3c_master_controller *m = i3c_dev_get_master(dev); 1923 struct svc_i3c_master *master = to_svc_i3c_master(m); 1924 int ret; 1925 1926 master->enabled_events--; 1927 if (!master->enabled_events) 1928 svc_i3c_master_disable_interrupts(master); 1929 1930 ret = i3c_master_disec_locked(m, dev->info.dyn_addr, I3C_CCC_EVENT_SIR); 1931 1932 pm_runtime_put_autosuspend(master->dev); 1933 1934 return ret; 1935 } 1936 1937 static int svc_i3c_master_enable_hotjoin(struct i3c_master_controller *m) 1938 { 1939 struct svc_i3c_master *master = to_svc_i3c_master(m); 1940 int ret; 1941 1942 ret = pm_runtime_resume_and_get(master->dev); 1943 if (ret < 0) { 1944 dev_err(master->dev, "<%s> Cannot get runtime PM.\n", __func__); 1945 return ret; 1946 } 1947 1948 master->enabled_events |= SVC_I3C_EVENT_HOTJOIN; 1949 1950 svc_i3c_master_enable_interrupts(master, SVC_I3C_MINT_SLVSTART); 1951 1952 return 0; 1953 } 1954 1955 static int svc_i3c_master_disable_hotjoin(struct i3c_master_controller *m) 1956 { 1957 struct svc_i3c_master *master = to_svc_i3c_master(m); 1958 1959 master->enabled_events &= ~SVC_I3C_EVENT_HOTJOIN; 1960 1961 if (!master->enabled_events) 1962 svc_i3c_master_disable_interrupts(master); 1963 1964 pm_runtime_put_autosuspend(master->dev); 1965 1966 return 0; 1967 } 1968 1969 static void svc_i3c_master_recycle_ibi_slot(struct i3c_dev_desc *dev, 1970 struct i3c_ibi_slot *slot) 1971 { 1972 struct svc_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev); 1973 1974 i3c_generic_ibi_recycle_slot(data->ibi_pool, slot); 1975 } 1976 1977 static const struct i3c_master_controller_ops svc_i3c_master_ops = { 1978 .bus_init = svc_i3c_master_bus_init, 1979 .bus_cleanup = svc_i3c_master_bus_cleanup, 1980 .attach_i3c_dev = svc_i3c_master_attach_i3c_dev, 1981 .detach_i3c_dev = svc_i3c_master_detach_i3c_dev, 1982 .reattach_i3c_dev = svc_i3c_master_reattach_i3c_dev, 1983 .attach_i2c_dev = svc_i3c_master_attach_i2c_dev, 1984 .detach_i2c_dev = svc_i3c_master_detach_i2c_dev, 1985 .do_daa = svc_i3c_master_do_daa, 1986 .supports_ccc_cmd = svc_i3c_master_supports_ccc_cmd, 1987 .send_ccc_cmd = svc_i3c_master_send_ccc_cmd, 1988 .i3c_xfers = svc_i3c_master_i3c_xfers, 1989 .i2c_xfers = svc_i3c_master_i2c_xfers, 1990 .request_ibi = svc_i3c_master_request_ibi, 1991 .free_ibi = svc_i3c_master_free_ibi, 1992 .recycle_ibi_slot = svc_i3c_master_recycle_ibi_slot, 1993 .enable_ibi = svc_i3c_master_enable_ibi, 1994 .disable_ibi = svc_i3c_master_disable_ibi, 1995 .enable_hotjoin = svc_i3c_master_enable_hotjoin, 1996 .disable_hotjoin = svc_i3c_master_disable_hotjoin, 1997 .set_speed = svc_i3c_master_set_speed, 1998 }; 1999 2000 static int svc_i3c_master_probe(struct platform_device *pdev) 2001 { 2002 struct device *dev = &pdev->dev; 2003 struct svc_i3c_master *master; 2004 int ret, i; 2005 2006 master = devm_kzalloc(dev, sizeof(*master), GFP_KERNEL); 2007 if (!master) 2008 return -ENOMEM; 2009 2010 master->drvdata = of_device_get_match_data(dev); 2011 if (!master->drvdata) 2012 return -EINVAL; 2013 2014 master->regs = devm_platform_ioremap_resource(pdev, 0); 2015 if (IS_ERR(master->regs)) 2016 return PTR_ERR(master->regs); 2017 2018 master->num_clks = devm_clk_bulk_get_all(dev, &master->clks); 2019 if (master->num_clks < 0) 2020 return dev_err_probe(dev, -EINVAL, "can't get I3C clocks\n"); 2021 2022 for (i = 0; i < master->num_clks; i++) { 2023 if (!strcmp(master->clks[i].id, "fast_clk")) 2024 break; 2025 } 2026 2027 if (i == master->num_clks) 2028 return dev_err_probe(dev, -EINVAL, 2029 "can't get I3C peripheral clock\n"); 2030 2031 master->fclk = master->clks[i].clk; 2032 if (IS_ERR(master->fclk)) 2033 return PTR_ERR(master->fclk); 2034 2035 master->irq = platform_get_irq(pdev, 0); 2036 if (master->irq < 0) 2037 return master->irq; 2038 2039 master->dev = dev; 2040 ret = clk_bulk_prepare_enable(master->num_clks, master->clks); 2041 if (ret) 2042 return dev_err_probe(dev, ret, "can't enable I3C clocks\n"); 2043 2044 mutex_init(&master->lock); 2045 2046 ret = devm_request_irq(dev, master->irq, svc_i3c_master_irq_handler, 2047 IRQF_NO_SUSPEND, "svc-i3c-irq", master); 2048 if (ret) 2049 goto err_disable_clks; 2050 2051 master->free_slots = GENMASK(SVC_I3C_MAX_DEVS - 1, 0); 2052 2053 spin_lock_init(&master->xferqueue.lock); 2054 INIT_LIST_HEAD(&master->xferqueue.list); 2055 2056 spin_lock_init(&master->ibi.lock); 2057 master->ibi.num_slots = SVC_I3C_MAX_DEVS; 2058 master->ibi.slots = devm_kcalloc(&pdev->dev, master->ibi.num_slots, 2059 sizeof(*master->ibi.slots), 2060 GFP_KERNEL); 2061 if (!master->ibi.slots) { 2062 ret = -ENOMEM; 2063 goto err_disable_clks; 2064 } 2065 2066 platform_set_drvdata(pdev, master); 2067 2068 pm_runtime_set_autosuspend_delay(&pdev->dev, SVC_I3C_PM_TIMEOUT_MS); 2069 pm_runtime_use_autosuspend(&pdev->dev); 2070 pm_runtime_get_noresume(&pdev->dev); 2071 pm_runtime_set_active(&pdev->dev); 2072 pm_runtime_enable(&pdev->dev); 2073 2074 svc_i3c_master_reset(master); 2075 2076 /* Register the master */ 2077 ret = i3c_master_register(&master->base, &pdev->dev, 2078 &svc_i3c_master_ops, false); 2079 if (ret) 2080 goto rpm_disable; 2081 2082 pm_runtime_put_autosuspend(&pdev->dev); 2083 2084 return 0; 2085 2086 rpm_disable: 2087 pm_runtime_dont_use_autosuspend(&pdev->dev); 2088 pm_runtime_put_noidle(&pdev->dev); 2089 pm_runtime_disable(&pdev->dev); 2090 pm_runtime_set_suspended(&pdev->dev); 2091 2092 err_disable_clks: 2093 clk_bulk_disable_unprepare(master->num_clks, master->clks); 2094 2095 return ret; 2096 } 2097 2098 static void svc_i3c_master_remove(struct platform_device *pdev) 2099 { 2100 struct svc_i3c_master *master = platform_get_drvdata(pdev); 2101 2102 i3c_master_unregister(&master->base); 2103 2104 pm_runtime_dont_use_autosuspend(&pdev->dev); 2105 pm_runtime_disable(&pdev->dev); 2106 } 2107 2108 static void svc_i3c_save_regs(struct svc_i3c_master *master) 2109 { 2110 master->saved_regs.mconfig = readl(master->regs + SVC_I3C_MCONFIG); 2111 master->saved_regs.mdynaddr = readl(master->regs + SVC_I3C_MDYNADDR); 2112 } 2113 2114 static void svc_i3c_restore_regs(struct svc_i3c_master *master) 2115 { 2116 if (readl(master->regs + SVC_I3C_MDYNADDR) != 2117 master->saved_regs.mdynaddr) { 2118 writel(master->saved_regs.mconfig, 2119 master->regs + SVC_I3C_MCONFIG); 2120 writel(master->saved_regs.mdynaddr, 2121 master->regs + SVC_I3C_MDYNADDR); 2122 } 2123 } 2124 2125 static int __maybe_unused svc_i3c_runtime_suspend(struct device *dev) 2126 { 2127 struct svc_i3c_master *master = dev_get_drvdata(dev); 2128 2129 svc_i3c_save_regs(master); 2130 clk_bulk_disable_unprepare(master->num_clks, master->clks); 2131 pinctrl_pm_select_sleep_state(dev); 2132 2133 return 0; 2134 } 2135 2136 static int __maybe_unused svc_i3c_runtime_resume(struct device *dev) 2137 { 2138 struct svc_i3c_master *master = dev_get_drvdata(dev); 2139 int ret; 2140 2141 pinctrl_pm_select_default_state(dev); 2142 ret = clk_bulk_prepare_enable(master->num_clks, master->clks); 2143 if (ret) 2144 return ret; 2145 2146 svc_i3c_restore_regs(master); 2147 2148 return 0; 2149 } 2150 2151 static const struct dev_pm_ops svc_i3c_pm_ops = { 2152 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 2153 pm_runtime_force_resume) 2154 SET_RUNTIME_PM_OPS(svc_i3c_runtime_suspend, 2155 svc_i3c_runtime_resume, NULL) 2156 }; 2157 2158 static const struct svc_i3c_drvdata npcm845_drvdata = { 2159 .quirks = SVC_I3C_QUIRK_FIFO_EMPTY | 2160 SVC_I3C_QUIRK_FALSE_SLVSTART | 2161 SVC_I3C_QUIRK_DAA_CORRUPT, 2162 }; 2163 2164 static const struct svc_i3c_drvdata svc_default_drvdata = {}; 2165 2166 static const struct of_device_id svc_i3c_master_of_match_tbl[] = { 2167 { .compatible = "nuvoton,npcm845-i3c", .data = &npcm845_drvdata }, 2168 { .compatible = "silvaco,i3c-master-v1", .data = &svc_default_drvdata }, 2169 { /* sentinel */ }, 2170 }; 2171 MODULE_DEVICE_TABLE(of, svc_i3c_master_of_match_tbl); 2172 2173 static struct platform_driver svc_i3c_master = { 2174 .probe = svc_i3c_master_probe, 2175 .remove = svc_i3c_master_remove, 2176 .driver = { 2177 .name = "silvaco-i3c-master", 2178 .of_match_table = svc_i3c_master_of_match_tbl, 2179 .pm = &svc_i3c_pm_ops, 2180 }, 2181 }; 2182 module_platform_driver(svc_i3c_master); 2183 2184 MODULE_AUTHOR("Conor Culhane <conor.culhane@silvaco.com>"); 2185 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>"); 2186 MODULE_DESCRIPTION("Silvaco dual-role I3C master driver"); 2187 MODULE_LICENSE("GPL v2"); 2188