1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Microchip PolarFire SoC (MPFS) system controller driver 4 * 5 * Copyright (c) 2020-2021 Microchip Corporation. All rights reserved. 6 * 7 * Author: Conor Dooley <conor.dooley@microchip.com> 8 * 9 */ 10 11 #include <linux/slab.h> 12 #include <linux/kref.h> 13 #include <linux/module.h> 14 #include <linux/jiffies.h> 15 #include <linux/mtd/mtd.h> 16 #include <linux/spi/spi.h> 17 #include <linux/interrupt.h> 18 #include <linux/of.h> 19 #include <linux/mailbox_client.h> 20 #include <linux/platform_device.h> 21 #include <soc/microchip/mpfs.h> 22 23 /* 24 * This timeout must be long, as some services (example: image authentication) 25 * take significant time to complete 26 */ 27 #define MPFS_SYS_CTRL_TIMEOUT_MS 30000 28 29 static DEFINE_MUTEX(transaction_lock); 30 31 struct mpfs_sys_controller { 32 struct mbox_client client; 33 struct mbox_chan *chan; 34 struct completion c; 35 struct mtd_info *flash; 36 struct kref consumers; 37 }; 38 39 struct mpfs_syscon_config { 40 unsigned int nb_subdevs; 41 struct platform_device *subdevs; 42 }; 43 44 int mpfs_blocking_transaction(struct mpfs_sys_controller *sys_controller, struct mpfs_mss_msg *msg) 45 { 46 unsigned long timeout = msecs_to_jiffies(MPFS_SYS_CTRL_TIMEOUT_MS); 47 int ret; 48 49 ret = mutex_lock_interruptible(&transaction_lock); 50 if (ret) 51 return ret; 52 53 reinit_completion(&sys_controller->c); 54 55 ret = mbox_send_message(sys_controller->chan, msg); 56 if (ret < 0) { 57 dev_warn(sys_controller->client.dev, "MPFS sys controller service timeout\n"); 58 goto out; 59 } 60 61 /* 62 * Unfortunately, the system controller will only deliver an interrupt 63 * if a service succeeds. mbox_send_message() will block until the busy 64 * flag is gone. If the busy flag is gone but no interrupt has arrived 65 * to trigger the rx callback then the service can be deemed to have 66 * failed. 67 * The caller can then interrogate msg::response::resp_status to 68 * determine the cause of the failure. 69 * mbox_send_message() returns positive integers in the success path, so 70 * ret needs to be cleared if we do get an interrupt. 71 */ 72 if (!wait_for_completion_timeout(&sys_controller->c, timeout)) { 73 ret = -EBADMSG; 74 dev_warn(sys_controller->client.dev, 75 "MPFS sys controller service failed with status: %d\n", 76 msg->response->resp_status); 77 } else { 78 ret = 0; 79 } 80 81 out: 82 mutex_unlock(&transaction_lock); 83 84 return ret; 85 } 86 EXPORT_SYMBOL(mpfs_blocking_transaction); 87 88 static void mpfs_sys_controller_rx_callback(struct mbox_client *client, void *msg) 89 { 90 struct mpfs_sys_controller *sys_controller = 91 container_of(client, struct mpfs_sys_controller, client); 92 93 complete(&sys_controller->c); 94 } 95 96 static void mpfs_sys_controller_delete(struct kref *kref) 97 { 98 struct mpfs_sys_controller *sys_controller = 99 container_of(kref, struct mpfs_sys_controller, consumers); 100 101 mbox_free_channel(sys_controller->chan); 102 kfree(sys_controller); 103 } 104 105 static void mpfs_sys_controller_put(void *data) 106 { 107 struct mpfs_sys_controller *sys_controller = data; 108 109 kref_put(&sys_controller->consumers, mpfs_sys_controller_delete); 110 } 111 112 struct mtd_info *mpfs_sys_controller_get_flash(struct mpfs_sys_controller *mpfs_client) 113 { 114 return mpfs_client->flash; 115 } 116 EXPORT_SYMBOL(mpfs_sys_controller_get_flash); 117 118 static int mpfs_sys_controller_probe(struct platform_device *pdev) 119 { 120 struct device *dev = &pdev->dev; 121 struct mpfs_sys_controller *sys_controller; 122 struct mpfs_syscon_config *of_data; 123 struct device_node *np; 124 int i, ret; 125 126 sys_controller = kzalloc_obj(*sys_controller); 127 if (!sys_controller) 128 return -ENOMEM; 129 130 np = of_parse_phandle(dev->of_node, "microchip,bitstream-flash", 0); 131 if (!np) 132 goto no_flash; 133 134 sys_controller->flash = of_get_mtd_device_by_node(np); 135 of_node_put(np); 136 if (IS_ERR(sys_controller->flash)) { 137 ret = dev_err_probe(dev, PTR_ERR(sys_controller->flash), "Failed to get flash\n"); 138 goto out_free; 139 } 140 141 no_flash: 142 sys_controller->client.dev = dev; 143 sys_controller->client.rx_callback = mpfs_sys_controller_rx_callback; 144 sys_controller->client.tx_block = 1U; 145 sys_controller->client.tx_tout = msecs_to_jiffies(MPFS_SYS_CTRL_TIMEOUT_MS); 146 147 sys_controller->chan = mbox_request_channel(&sys_controller->client, 0); 148 if (IS_ERR(sys_controller->chan)) { 149 ret = dev_err_probe(dev, PTR_ERR(sys_controller->chan), 150 "Failed to get mbox channel\n"); 151 goto out_free; 152 } 153 154 init_completion(&sys_controller->c); 155 kref_init(&sys_controller->consumers); 156 157 platform_set_drvdata(pdev, sys_controller); 158 159 of_data = (struct mpfs_syscon_config *) device_get_match_data(dev); 160 if (!of_data) { 161 dev_err(dev, "Error getting match data\n"); 162 return -EINVAL; 163 } 164 165 for (i = 0; i < of_data->nb_subdevs; i++) { 166 of_data->subdevs[i].dev.parent = dev; 167 if (platform_device_register(&of_data->subdevs[i])) 168 dev_warn(dev, "Error registering sub device %s\n", 169 of_data->subdevs[i].name); 170 } 171 172 dev_info(&pdev->dev, "Registered MPFS system controller\n"); 173 174 return 0; 175 176 out_free: 177 kfree(sys_controller); 178 return ret; 179 } 180 181 static void mpfs_sys_controller_remove(struct platform_device *pdev) 182 { 183 struct mpfs_sys_controller *sys_controller = platform_get_drvdata(pdev); 184 185 mpfs_sys_controller_put(sys_controller); 186 } 187 188 static struct platform_device mpfs_subdevs[] = { 189 { 190 .name = "mpfs-rng", 191 .id = -1, 192 }, 193 { 194 .name = "mpfs-generic-service", 195 .id = -1, 196 }, 197 { 198 .name = "mpfs-auto-update", 199 .id = -1, 200 }, 201 }; 202 203 static struct platform_device pic64gx_subdevs[] = { 204 { 205 .name = "mpfs-rng", 206 .id = -1, 207 }, 208 { 209 .name = "mpfs-generic-service", 210 .id = -1, 211 }, 212 }; 213 214 static const struct mpfs_syscon_config mpfs_config = { 215 .nb_subdevs = ARRAY_SIZE(mpfs_subdevs), 216 .subdevs = mpfs_subdevs, 217 }; 218 219 static const struct mpfs_syscon_config pic64gx_config = { 220 .nb_subdevs = ARRAY_SIZE(pic64gx_subdevs), 221 .subdevs = pic64gx_subdevs, 222 }; 223 224 static const struct of_device_id mpfs_sys_controller_of_match[] = { 225 {.compatible = "microchip,mpfs-sys-controller", .data = &mpfs_config}, 226 {.compatible = "microchip,pic64gx-sys-controller", .data = &pic64gx_config}, 227 {}, 228 }; 229 MODULE_DEVICE_TABLE(of, mpfs_sys_controller_of_match); 230 231 struct mpfs_sys_controller *mpfs_sys_controller_get(struct device *dev) 232 { 233 const struct of_device_id *match; 234 struct mpfs_sys_controller *sys_controller; 235 int ret; 236 237 if (!dev->parent) 238 goto err_no_device; 239 240 match = of_match_node(mpfs_sys_controller_of_match, dev->parent->of_node); 241 of_node_put(dev->parent->of_node); 242 if (!match) 243 goto err_no_device; 244 245 sys_controller = dev_get_drvdata(dev->parent); 246 if (!sys_controller) 247 goto err_bad_device; 248 249 if (!kref_get_unless_zero(&sys_controller->consumers)) 250 goto err_bad_device; 251 252 ret = devm_add_action_or_reset(dev, mpfs_sys_controller_put, sys_controller); 253 if (ret) 254 return ERR_PTR(ret); 255 256 return sys_controller; 257 258 err_no_device: 259 dev_dbg(dev, "Parent device was not an MPFS system controller\n"); 260 return ERR_PTR(-ENODEV); 261 262 err_bad_device: 263 dev_dbg(dev, "MPFS system controller found but could not register as a sub device\n"); 264 return ERR_PTR(-EPROBE_DEFER); 265 } 266 EXPORT_SYMBOL(mpfs_sys_controller_get); 267 268 static struct platform_driver mpfs_sys_controller_driver = { 269 .driver = { 270 .name = "mpfs-sys-controller", 271 .of_match_table = mpfs_sys_controller_of_match, 272 }, 273 .probe = mpfs_sys_controller_probe, 274 .remove = mpfs_sys_controller_remove, 275 }; 276 module_platform_driver(mpfs_sys_controller_driver); 277 278 MODULE_LICENSE("GPL v2"); 279 MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>"); 280 MODULE_DESCRIPTION("MPFS system controller driver"); 281