Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * PS3 virtual uart |
| 3 | * |
| 4 | * Copyright (C) 2006 Sony Computer Entertainment Inc. |
| 5 | * Copyright 2006 Sony Corp. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/interrupt.h> |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 26 | #include <linux/bitops.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 27 | #include <asm/ps3.h> |
| 28 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 29 | #include <asm/firmware.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 30 | #include <asm/lv1call.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 31 | |
| 32 | #include "vuart.h" |
| 33 | |
| 34 | MODULE_AUTHOR("Sony Corporation"); |
| 35 | MODULE_LICENSE("GPL v2"); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 36 | MODULE_DESCRIPTION("PS3 vuart"); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * vuart - An inter-partition data link service. |
| 40 | * port 0: PS3 AV Settings. |
| 41 | * port 2: PS3 System Manager. |
| 42 | * |
| 43 | * The vuart provides a bi-directional byte stream data link between logical |
| 44 | * partitions. Its primary role is as a communications link between the guest |
| 45 | * OS and the system policy module. The current HV does not support any |
| 46 | * connections other than those listed. |
| 47 | */ |
| 48 | |
| 49 | enum {PORT_COUNT = 3,}; |
| 50 | |
| 51 | enum vuart_param { |
| 52 | PARAM_TX_TRIGGER = 0, |
| 53 | PARAM_RX_TRIGGER = 1, |
| 54 | PARAM_INTERRUPT_MASK = 2, |
| 55 | PARAM_RX_BUF_SIZE = 3, /* read only */ |
| 56 | PARAM_RX_BYTES = 4, /* read only */ |
| 57 | PARAM_TX_BUF_SIZE = 5, /* read only */ |
| 58 | PARAM_TX_BYTES = 6, /* read only */ |
| 59 | PARAM_INTERRUPT_STATUS = 7, /* read only */ |
| 60 | }; |
| 61 | |
| 62 | enum vuart_interrupt_bit { |
| 63 | INTERRUPT_BIT_TX = 0, |
| 64 | INTERRUPT_BIT_RX = 1, |
| 65 | INTERRUPT_BIT_DISCONNECT = 2, |
| 66 | }; |
| 67 | |
| 68 | enum vuart_interrupt_mask { |
| 69 | INTERRUPT_MASK_TX = 1, |
| 70 | INTERRUPT_MASK_RX = 2, |
| 71 | INTERRUPT_MASK_DISCONNECT = 4, |
| 72 | }; |
| 73 | |
| 74 | /** |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 75 | * struct ps3_vuart_port_priv - private vuart device data. |
| 76 | */ |
| 77 | |
| 78 | struct ps3_vuart_port_priv { |
| 79 | u64 interrupt_mask; |
| 80 | |
| 81 | struct { |
| 82 | spinlock_t lock; |
| 83 | struct list_head head; |
| 84 | } tx_list; |
| 85 | struct { |
| 86 | struct ps3_vuart_work work; |
| 87 | unsigned long bytes_held; |
| 88 | spinlock_t lock; |
| 89 | struct list_head head; |
| 90 | } rx_list; |
| 91 | struct ps3_vuart_stats stats; |
| 92 | }; |
| 93 | |
| 94 | static struct ps3_vuart_port_priv *to_port_priv( |
| 95 | struct ps3_system_bus_device *dev) |
| 96 | { |
| 97 | BUG_ON(!dev); |
| 98 | BUG_ON(!dev->driver_priv); |
| 99 | return (struct ps3_vuart_port_priv *)dev->driver_priv; |
| 100 | } |
| 101 | |
| 102 | /** |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 103 | * struct ports_bmp - bitmap indicating ports needing service. |
| 104 | * |
| 105 | * A 256 bit read only bitmap indicating ports needing service. Do not write |
| 106 | * to these bits. Must not cross a page boundary. |
| 107 | */ |
| 108 | |
| 109 | struct ports_bmp { |
| 110 | u64 status; |
| 111 | u64 unused[3]; |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 112 | } __attribute__((aligned(32))); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 113 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 114 | #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__) |
Geoff Levand | 848cfdc | 2007-06-16 07:18:14 +1000 | [diff] [blame] | 115 | static void __maybe_unused _dump_ports_bmp( |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 116 | const struct ports_bmp *bmp, const char *func, int line) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 117 | { |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 118 | pr_debug("%s:%d: ports_bmp: %016llxh\n", func, line, bmp->status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 121 | #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__) |
Geoff Levand | 848cfdc | 2007-06-16 07:18:14 +1000 | [diff] [blame] | 122 | static void __maybe_unused _dump_port_params(unsigned int port_number, |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 123 | const char *func, int line) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 124 | { |
| 125 | #if defined(DEBUG) |
| 126 | static const char *strings[] = { |
| 127 | "tx_trigger ", |
| 128 | "rx_trigger ", |
| 129 | "interrupt_mask ", |
| 130 | "rx_buf_size ", |
| 131 | "rx_bytes ", |
| 132 | "tx_buf_size ", |
| 133 | "tx_bytes ", |
| 134 | "interrupt_status", |
| 135 | }; |
| 136 | int result; |
| 137 | unsigned int i; |
| 138 | u64 value; |
| 139 | |
| 140 | for (i = 0; i < ARRAY_SIZE(strings); i++) { |
| 141 | result = lv1_get_virtual_uart_param(port_number, i, &value); |
| 142 | |
| 143 | if (result) { |
| 144 | pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line, |
| 145 | port_number, strings[i], ps3_result(result)); |
| 146 | continue; |
| 147 | } |
| 148 | pr_debug("%s:%d: port_%u: %s = %lxh\n", |
| 149 | func, line, port_number, strings[i], value); |
| 150 | } |
| 151 | #endif |
| 152 | } |
| 153 | |
| 154 | struct vuart_triggers { |
| 155 | unsigned long rx; |
| 156 | unsigned long tx; |
| 157 | }; |
| 158 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 159 | int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 160 | struct vuart_triggers *trig) |
| 161 | { |
| 162 | int result; |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 163 | u64 size; |
| 164 | u64 val; |
| 165 | u64 tx; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 166 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 167 | result = lv1_get_virtual_uart_param(dev->port_number, |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 168 | PARAM_TX_TRIGGER, &tx); |
| 169 | trig->tx = tx; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 170 | |
| 171 | if (result) { |
| 172 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", |
| 173 | __func__, __LINE__, ps3_result(result)); |
| 174 | return result; |
| 175 | } |
| 176 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 177 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 178 | PARAM_RX_BUF_SIZE, &size); |
| 179 | |
| 180 | if (result) { |
| 181 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", |
| 182 | __func__, __LINE__, ps3_result(result)); |
| 183 | return result; |
| 184 | } |
| 185 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 186 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 187 | PARAM_RX_TRIGGER, &val); |
| 188 | |
| 189 | if (result) { |
| 190 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", |
| 191 | __func__, __LINE__, ps3_result(result)); |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | trig->rx = size - val; |
| 196 | |
| 197 | dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__, |
| 198 | trig->tx, trig->rx); |
| 199 | |
| 200 | return result; |
| 201 | } |
| 202 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 203 | int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 204 | unsigned int rx) |
| 205 | { |
| 206 | int result; |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 207 | u64 size; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 208 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 209 | result = lv1_set_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 210 | PARAM_TX_TRIGGER, tx); |
| 211 | |
| 212 | if (result) { |
| 213 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", |
| 214 | __func__, __LINE__, ps3_result(result)); |
| 215 | return result; |
| 216 | } |
| 217 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 218 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 219 | PARAM_RX_BUF_SIZE, &size); |
| 220 | |
| 221 | if (result) { |
| 222 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", |
| 223 | __func__, __LINE__, ps3_result(result)); |
| 224 | return result; |
| 225 | } |
| 226 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 227 | result = lv1_set_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 228 | PARAM_RX_TRIGGER, size - rx); |
| 229 | |
| 230 | if (result) { |
| 231 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", |
| 232 | __func__, __LINE__, ps3_result(result)); |
| 233 | return result; |
| 234 | } |
| 235 | |
| 236 | dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__, |
| 237 | tx, rx); |
| 238 | |
| 239 | return result; |
| 240 | } |
| 241 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 242 | static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 243 | u64 *bytes_waiting) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 244 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 245 | int result; |
| 246 | |
| 247 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 248 | PARAM_RX_BYTES, bytes_waiting); |
| 249 | |
| 250 | if (result) |
| 251 | dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n", |
| 252 | __func__, __LINE__, ps3_result(result)); |
| 253 | |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 254 | dev_dbg(&dev->core, "%s:%d: %llxh\n", __func__, __LINE__, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 255 | *bytes_waiting); |
| 256 | return result; |
| 257 | } |
| 258 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 259 | /** |
| 260 | * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources. |
| 261 | * @dev: The struct ps3_system_bus_device instance. |
| 262 | * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables. |
| 263 | */ |
| 264 | |
| 265 | static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 266 | unsigned long mask) |
| 267 | { |
| 268 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 269 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 270 | |
| 271 | dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask); |
| 272 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 273 | priv->interrupt_mask = mask; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 274 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 275 | result = lv1_set_virtual_uart_param(dev->port_number, |
| 276 | PARAM_INTERRUPT_MASK, priv->interrupt_mask); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 277 | |
| 278 | if (result) |
| 279 | dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n", |
| 280 | __func__, __LINE__, ps3_result(result)); |
| 281 | |
| 282 | return result; |
| 283 | } |
| 284 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 285 | static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 286 | unsigned long *status) |
| 287 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 288 | int result; |
| 289 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 290 | u64 tmp; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 291 | |
| 292 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 293 | PARAM_INTERRUPT_STATUS, &tmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 294 | |
| 295 | if (result) |
| 296 | dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n", |
| 297 | __func__, __LINE__, ps3_result(result)); |
| 298 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 299 | *status = tmp & priv->interrupt_mask; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 300 | |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 301 | dev_dbg(&dev->core, "%s:%d: m %llxh, s %llxh, m&s %lxh\n", |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 302 | __func__, __LINE__, priv->interrupt_mask, tmp, *status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 303 | |
| 304 | return result; |
| 305 | } |
| 306 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 307 | int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 308 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 309 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 310 | |
| 311 | return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0 |
| 312 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 313 | | INTERRUPT_MASK_TX); |
| 314 | } |
| 315 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 316 | int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 317 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 318 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 319 | |
| 320 | return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0 |
| 321 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 322 | | INTERRUPT_MASK_RX); |
| 323 | } |
| 324 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 325 | int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 326 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 327 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 328 | |
| 329 | return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0 |
| 330 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 331 | | INTERRUPT_MASK_DISCONNECT); |
| 332 | } |
| 333 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 334 | int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 335 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 336 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 337 | |
| 338 | return (priv->interrupt_mask & INTERRUPT_MASK_TX) |
| 339 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 340 | & ~INTERRUPT_MASK_TX) : 0; |
| 341 | } |
| 342 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 343 | int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 344 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 345 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 346 | |
| 347 | return (priv->interrupt_mask & INTERRUPT_MASK_RX) |
| 348 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 349 | & ~INTERRUPT_MASK_RX) : 0; |
| 350 | } |
| 351 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 352 | int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 353 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 354 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 355 | |
| 356 | return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) |
| 357 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 358 | & ~INTERRUPT_MASK_DISCONNECT) : 0; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * ps3_vuart_raw_write - Low level write helper. |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 363 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 364 | * |
| 365 | * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write. |
| 366 | */ |
| 367 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 368 | static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev, |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 369 | const void *buf, unsigned int bytes, u64 *bytes_written) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 370 | { |
| 371 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 372 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 373 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 374 | result = lv1_write_virtual_uart(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 375 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written); |
| 376 | |
| 377 | if (result) { |
| 378 | dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: " |
| 379 | "%s\n", __func__, __LINE__, ps3_result(result)); |
| 380 | return result; |
| 381 | } |
| 382 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 383 | priv->stats.bytes_written += *bytes_written; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 384 | |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 385 | dev_dbg(&dev->core, "%s:%d: wrote %llxh/%xh=>%lxh\n", __func__, __LINE__, |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 386 | *bytes_written, bytes, priv->stats.bytes_written); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 387 | |
| 388 | return result; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * ps3_vuart_raw_read - Low level read helper. |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 393 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 394 | * |
| 395 | * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read. |
| 396 | */ |
| 397 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 398 | static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf, |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 399 | unsigned int bytes, u64 *bytes_read) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 400 | { |
| 401 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 402 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 403 | |
| 404 | dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes); |
| 405 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 406 | result = lv1_read_virtual_uart(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 407 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read); |
| 408 | |
| 409 | if (result) { |
| 410 | dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n", |
| 411 | __func__, __LINE__, ps3_result(result)); |
| 412 | return result; |
| 413 | } |
| 414 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 415 | priv->stats.bytes_read += *bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 416 | |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 417 | dev_dbg(&dev->core, "%s:%d: read %llxh/%xh=>%lxh\n", __func__, __LINE__, |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 418 | *bytes_read, bytes, priv->stats.bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 419 | |
| 420 | return result; |
| 421 | } |
| 422 | |
| 423 | /** |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 424 | * ps3_vuart_clear_rx_bytes - Discard bytes received. |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 425 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 426 | * @bytes: Max byte count to discard, zero = all pending. |
| 427 | * |
| 428 | * Used to clear pending rx interrupt source. Will not block. |
| 429 | */ |
| 430 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 431 | void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 432 | unsigned int bytes) |
| 433 | { |
| 434 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 435 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 436 | u64 bytes_waiting; |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 437 | void *tmp; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 438 | |
| 439 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting); |
| 440 | |
| 441 | BUG_ON(result); |
| 442 | |
| 443 | bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting; |
| 444 | |
| 445 | dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes); |
| 446 | |
| 447 | if (!bytes) |
| 448 | return; |
| 449 | |
| 450 | /* Add some extra space for recently arrived data. */ |
| 451 | |
| 452 | bytes += 128; |
| 453 | |
| 454 | tmp = kmalloc(bytes, GFP_KERNEL); |
| 455 | |
| 456 | if (!tmp) |
| 457 | return; |
| 458 | |
| 459 | ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting); |
| 460 | |
| 461 | kfree(tmp); |
| 462 | |
| 463 | /* Don't include these bytes in the stats. */ |
| 464 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 465 | priv->stats.bytes_read -= bytes_waiting; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 466 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 467 | EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 468 | |
| 469 | /** |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 470 | * struct list_buffer - An element for a port device fifo buffer list. |
| 471 | */ |
| 472 | |
| 473 | struct list_buffer { |
| 474 | struct list_head link; |
| 475 | const unsigned char *head; |
| 476 | const unsigned char *tail; |
| 477 | unsigned long dbg_number; |
| 478 | unsigned char data[]; |
| 479 | }; |
| 480 | |
| 481 | /** |
| 482 | * ps3_vuart_write - the entry point for writing data to a port |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 483 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 484 | * |
| 485 | * If the port is idle on entry as much of the incoming data is written to |
| 486 | * the port as the port will accept. Otherwise a list buffer is created |
| 487 | * and any remaning incoming data is copied to that buffer. The buffer is |
| 488 | * then enqueued for transmision via the transmit interrupt. |
| 489 | */ |
| 490 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 491 | int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 492 | unsigned int bytes) |
| 493 | { |
| 494 | static unsigned long dbg_number; |
| 495 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 496 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 497 | unsigned long flags; |
| 498 | struct list_buffer *lb; |
| 499 | |
| 500 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, |
| 501 | bytes, bytes); |
| 502 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 503 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 504 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 505 | if (list_empty(&priv->tx_list.head)) { |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 506 | u64 bytes_written; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 507 | |
| 508 | result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written); |
| 509 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 510 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 511 | |
| 512 | if (result) { |
| 513 | dev_dbg(&dev->core, |
| 514 | "%s:%d: ps3_vuart_raw_write failed\n", |
| 515 | __func__, __LINE__); |
| 516 | return result; |
| 517 | } |
| 518 | |
| 519 | if (bytes_written == bytes) { |
| 520 | dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n", |
| 521 | __func__, __LINE__, bytes); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | bytes -= bytes_written; |
| 526 | buf += bytes_written; |
| 527 | } else |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 528 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 529 | |
| 530 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL); |
| 531 | |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 532 | if (!lb) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 533 | return -ENOMEM; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 534 | |
| 535 | memcpy(lb->data, buf, bytes); |
| 536 | lb->head = lb->data; |
| 537 | lb->tail = lb->data + bytes; |
| 538 | lb->dbg_number = ++dbg_number; |
| 539 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 540 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
| 541 | list_add_tail(&lb->link, &priv->tx_list.head); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 542 | ps3_vuart_enable_interrupt_tx(dev); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 543 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 544 | |
| 545 | dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n", |
| 546 | __func__, __LINE__, lb->dbg_number, bytes); |
| 547 | |
| 548 | return 0; |
| 549 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 550 | EXPORT_SYMBOL_GPL(ps3_vuart_write); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 551 | |
| 552 | /** |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 553 | * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list. |
| 554 | * @dev: The struct ps3_system_bus_device instance. |
| 555 | * @bytes_queued: Number of bytes queued to the buffer list. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 556 | * |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 557 | * Must be called with priv->rx_list.lock held. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 558 | */ |
| 559 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 560 | static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev, |
| 561 | u64 *bytes_queued) |
| 562 | { |
| 563 | static unsigned long dbg_number; |
| 564 | int result; |
| 565 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 566 | struct list_buffer *lb; |
| 567 | u64 bytes; |
| 568 | |
| 569 | *bytes_queued = 0; |
| 570 | |
| 571 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes); |
| 572 | BUG_ON(result); |
| 573 | |
| 574 | if (result) |
| 575 | return -EIO; |
| 576 | |
| 577 | if (!bytes) |
| 578 | return 0; |
| 579 | |
| 580 | /* Add some extra space for recently arrived data. */ |
| 581 | |
| 582 | bytes += 128; |
| 583 | |
| 584 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC); |
| 585 | |
| 586 | if (!lb) |
| 587 | return -ENOMEM; |
| 588 | |
| 589 | ps3_vuart_raw_read(dev, lb->data, bytes, &bytes); |
| 590 | |
| 591 | lb->head = lb->data; |
| 592 | lb->tail = lb->data + bytes; |
| 593 | lb->dbg_number = ++dbg_number; |
| 594 | |
| 595 | list_add_tail(&lb->link, &priv->rx_list.head); |
| 596 | priv->rx_list.bytes_held += bytes; |
| 597 | |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 598 | dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %llxh bytes\n", |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 599 | __func__, __LINE__, lb->dbg_number, bytes); |
| 600 | |
| 601 | *bytes_queued = bytes; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * ps3_vuart_read - The entry point for reading data from a port. |
| 608 | * |
| 609 | * Queue data waiting at the port, and if enough bytes to satisfy the request |
| 610 | * are held in the buffer list those bytes are dequeued and copied to the |
| 611 | * caller's buffer. Emptied list buffers are retiered. If the request cannot |
| 612 | * be statified by bytes held in the list buffers -EAGAIN is returned. |
| 613 | */ |
| 614 | |
| 615 | int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 616 | unsigned int bytes) |
| 617 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 618 | int result; |
| 619 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 620 | unsigned long flags; |
| 621 | struct list_buffer *lb, *n; |
| 622 | unsigned long bytes_read; |
| 623 | |
| 624 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, |
| 625 | bytes, bytes); |
| 626 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 627 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 628 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 629 | /* Queue rx bytes here for polled reads. */ |
| 630 | |
| 631 | while (priv->rx_list.bytes_held < bytes) { |
| 632 | u64 tmp; |
| 633 | |
| 634 | result = ps3_vuart_queue_rx_bytes(dev, &tmp); |
| 635 | if (result || !tmp) { |
| 636 | dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n", |
| 637 | __func__, __LINE__, |
| 638 | bytes - priv->rx_list.bytes_held); |
| 639 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
| 640 | return -EAGAIN; |
| 641 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 644 | list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 645 | bytes_read = min((unsigned int)(lb->tail - lb->head), bytes); |
| 646 | |
| 647 | memcpy(buf, lb->head, bytes_read); |
| 648 | buf += bytes_read; |
| 649 | bytes -= bytes_read; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 650 | priv->rx_list.bytes_held -= bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 651 | |
| 652 | if (bytes_read < lb->tail - lb->head) { |
| 653 | lb->head += bytes_read; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 654 | dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh " |
| 655 | "bytes\n", __func__, __LINE__, lb->dbg_number, |
| 656 | bytes_read); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 657 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 661 | dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh " |
| 662 | "bytes\n", __func__, __LINE__, lb->dbg_number, |
| 663 | bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 664 | |
| 665 | list_del(&lb->link); |
| 666 | kfree(lb); |
| 667 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 668 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 669 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 670 | return 0; |
| 671 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 672 | EXPORT_SYMBOL_GPL(ps3_vuart_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 673 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 674 | /** |
| 675 | * ps3_vuart_work - Asynchronous read handler. |
| 676 | */ |
| 677 | |
| 678 | static void ps3_vuart_work(struct work_struct *work) |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 679 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 680 | struct ps3_system_bus_device *dev = |
| 681 | ps3_vuart_work_to_system_bus_dev(work); |
| 682 | struct ps3_vuart_port_driver *drv = |
| 683 | ps3_system_bus_dev_to_vuart_drv(dev); |
| 684 | |
| 685 | BUG_ON(!drv); |
| 686 | drv->work(dev); |
| 687 | } |
| 688 | |
| 689 | int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes) |
| 690 | { |
| 691 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 692 | unsigned long flags; |
| 693 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 694 | if (priv->rx_list.work.trigger) { |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 695 | dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n", |
| 696 | __func__, __LINE__); |
| 697 | return -EAGAIN; |
| 698 | } |
| 699 | |
| 700 | BUG_ON(!bytes); |
| 701 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 702 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
| 703 | if (priv->rx_list.bytes_held >= bytes) { |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 704 | dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n", |
| 705 | __func__, __LINE__, bytes); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 706 | schedule_work(&priv->rx_list.work.work); |
| 707 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 708 | return 0; |
| 709 | } |
| 710 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 711 | priv->rx_list.work.trigger = bytes; |
| 712 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 713 | |
| 714 | dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__, |
| 715 | __LINE__, bytes, bytes); |
| 716 | |
| 717 | return 0; |
| 718 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 719 | EXPORT_SYMBOL_GPL(ps3_vuart_read_async); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 720 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 721 | void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev) |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 722 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 723 | to_port_priv(dev)->rx_list.work.trigger = 0; |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 724 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 725 | EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 726 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 727 | /** |
| 728 | * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler |
| 729 | * |
| 730 | * Services the transmit interrupt for the port. Writes as much data from the |
| 731 | * buffer list as the port will accept. Retires any emptied list buffers and |
| 732 | * adjusts the final list buffer state for a partial write. |
| 733 | */ |
| 734 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 735 | static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 736 | { |
| 737 | int result = 0; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 738 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 739 | unsigned long flags; |
| 740 | struct list_buffer *lb, *n; |
| 741 | unsigned long bytes_total = 0; |
| 742 | |
| 743 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 744 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 745 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 746 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 747 | list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 748 | |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 749 | u64 bytes_written; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 750 | |
| 751 | result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head, |
| 752 | &bytes_written); |
| 753 | |
| 754 | if (result) { |
| 755 | dev_dbg(&dev->core, |
| 756 | "%s:%d: ps3_vuart_raw_write failed\n", |
| 757 | __func__, __LINE__); |
| 758 | break; |
| 759 | } |
| 760 | |
| 761 | bytes_total += bytes_written; |
| 762 | |
| 763 | if (bytes_written < lb->tail - lb->head) { |
| 764 | lb->head += bytes_written; |
| 765 | dev_dbg(&dev->core, |
Stephen Rothwell | b17b3df | 2009-01-13 19:59:41 +0000 | [diff] [blame] | 766 | "%s:%d cleared buf_%lu, %llxh bytes\n", |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 767 | __func__, __LINE__, lb->dbg_number, |
| 768 | bytes_written); |
| 769 | goto port_full; |
| 770 | } |
| 771 | |
| 772 | dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__, |
| 773 | lb->dbg_number); |
| 774 | |
| 775 | list_del(&lb->link); |
| 776 | kfree(lb); |
| 777 | } |
| 778 | |
| 779 | ps3_vuart_disable_interrupt_tx(dev); |
| 780 | port_full: |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 781 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 782 | dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n", |
| 783 | __func__, __LINE__, bytes_total); |
| 784 | return result; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler |
| 789 | * |
| 790 | * Services the receive interrupt for the port. Creates a list buffer and |
| 791 | * copies all waiting port data to that buffer and enqueues the buffer in the |
| 792 | * buffer list. Buffer list data is dequeued via ps3_vuart_read. |
| 793 | */ |
| 794 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 795 | static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 796 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 797 | int result; |
| 798 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 799 | unsigned long flags; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 800 | u64 bytes; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 801 | |
| 802 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 803 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 804 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
| 805 | result = ps3_vuart_queue_rx_bytes(dev, &bytes); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 806 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 807 | if (result) { |
| 808 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
| 809 | return result; |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 810 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 811 | |
| 812 | if (priv->rx_list.work.trigger && priv->rx_list.bytes_held |
| 813 | >= priv->rx_list.work.trigger) { |
| 814 | dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n", |
| 815 | __func__, __LINE__, priv->rx_list.work.trigger); |
| 816 | priv->rx_list.work.trigger = 0; |
| 817 | schedule_work(&priv->rx_list.work.work); |
| 818 | } |
| 819 | |
| 820 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
| 821 | return result; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static int ps3_vuart_handle_interrupt_disconnect( |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 825 | struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 826 | { |
| 827 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 828 | BUG_ON("no support"); |
| 829 | return -1; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * ps3_vuart_handle_port_interrupt - second stage interrupt handler |
| 834 | * |
| 835 | * Services any pending interrupt types for the port. Passes control to the |
| 836 | * third stage type specific interrupt handler. Returns control to the first |
| 837 | * stage handler after one iteration. |
| 838 | */ |
| 839 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 840 | static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 841 | { |
| 842 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 843 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 844 | unsigned long status; |
| 845 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 846 | result = ps3_vuart_get_interrupt_status(dev, &status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 847 | |
| 848 | if (result) |
| 849 | return result; |
| 850 | |
| 851 | dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__, |
| 852 | status); |
| 853 | |
| 854 | if (status & INTERRUPT_MASK_DISCONNECT) { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 855 | priv->stats.disconnect_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 856 | result = ps3_vuart_handle_interrupt_disconnect(dev); |
| 857 | if (result) |
| 858 | ps3_vuart_disable_interrupt_disconnect(dev); |
| 859 | } |
| 860 | |
| 861 | if (status & INTERRUPT_MASK_TX) { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 862 | priv->stats.tx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 863 | result = ps3_vuart_handle_interrupt_tx(dev); |
| 864 | if (result) |
| 865 | ps3_vuart_disable_interrupt_tx(dev); |
| 866 | } |
| 867 | |
| 868 | if (status & INTERRUPT_MASK_RX) { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 869 | priv->stats.rx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 870 | result = ps3_vuart_handle_interrupt_rx(dev); |
| 871 | if (result) |
| 872 | ps3_vuart_disable_interrupt_rx(dev); |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 878 | struct vuart_bus_priv { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 879 | struct ports_bmp *bmp; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 880 | unsigned int virq; |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 881 | struct mutex probe_mutex; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 882 | int use_count; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 883 | struct ps3_system_bus_device *devices[PORT_COUNT]; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 884 | } static vuart_bus_priv; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 885 | |
| 886 | /** |
| 887 | * ps3_vuart_irq_handler - first stage interrupt handler |
| 888 | * |
| 889 | * Loops finding any interrupting port and its associated instance data. |
| 890 | * Passes control to the second stage port specific interrupt handler. Loops |
| 891 | * until all outstanding interrupts are serviced. |
| 892 | */ |
| 893 | |
| 894 | static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private) |
| 895 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 896 | struct vuart_bus_priv *bus_priv = _private; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 897 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 898 | BUG_ON(!bus_priv); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 899 | |
| 900 | while (1) { |
| 901 | unsigned int port; |
| 902 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 903 | dump_ports_bmp(bus_priv->bmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 904 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 905 | port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 906 | |
| 907 | if (port == BITS_PER_LONG) |
| 908 | break; |
| 909 | |
| 910 | BUG_ON(port >= PORT_COUNT); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 911 | BUG_ON(!bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 912 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 913 | ps3_vuart_handle_port_interrupt(bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | return IRQ_HANDLED; |
| 917 | } |
| 918 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 919 | static int ps3_vuart_bus_interrupt_get(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 920 | { |
| 921 | int result; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 922 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 923 | pr_debug(" -> %s:%d\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 924 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 925 | vuart_bus_priv.use_count++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 926 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 927 | BUG_ON(vuart_bus_priv.use_count > 2); |
| 928 | |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 929 | if (vuart_bus_priv.use_count != 1) |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 930 | return 0; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 931 | |
| 932 | BUG_ON(vuart_bus_priv.bmp); |
| 933 | |
| 934 | vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL); |
| 935 | |
| 936 | if (!vuart_bus_priv.bmp) { |
| 937 | pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__); |
| 938 | result = -ENOMEM; |
| 939 | goto fail_bmp_malloc; |
| 940 | } |
| 941 | |
| 942 | result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp, |
| 943 | &vuart_bus_priv.virq); |
| 944 | |
| 945 | if (result) { |
| 946 | pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n", |
| 947 | __func__, __LINE__, result); |
| 948 | result = -EPERM; |
| 949 | goto fail_alloc_irq; |
| 950 | } |
| 951 | |
| 952 | result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler, |
Yong Zhang | 6ea741a | 2011-10-21 23:56:53 +0000 | [diff] [blame] | 953 | 0, "vuart", &vuart_bus_priv); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 954 | |
| 955 | if (result) { |
| 956 | pr_debug("%s:%d: request_irq failed (%d)\n", |
| 957 | __func__, __LINE__, result); |
| 958 | goto fail_request_irq; |
| 959 | } |
| 960 | |
| 961 | pr_debug(" <- %s:%d: ok\n", __func__, __LINE__); |
| 962 | return result; |
| 963 | |
| 964 | fail_request_irq: |
| 965 | ps3_vuart_irq_destroy(vuart_bus_priv.virq); |
| 966 | vuart_bus_priv.virq = NO_IRQ; |
| 967 | fail_alloc_irq: |
| 968 | kfree(vuart_bus_priv.bmp); |
| 969 | vuart_bus_priv.bmp = NULL; |
| 970 | fail_bmp_malloc: |
| 971 | vuart_bus_priv.use_count--; |
| 972 | pr_debug(" <- %s:%d: failed\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 973 | return result; |
| 974 | } |
| 975 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 976 | static int ps3_vuart_bus_interrupt_put(void) |
| 977 | { |
| 978 | pr_debug(" -> %s:%d\n", __func__, __LINE__); |
| 979 | |
| 980 | vuart_bus_priv.use_count--; |
| 981 | |
| 982 | BUG_ON(vuart_bus_priv.use_count < 0); |
| 983 | |
| 984 | if (vuart_bus_priv.use_count != 0) |
| 985 | return 0; |
| 986 | |
| 987 | free_irq(vuart_bus_priv.virq, &vuart_bus_priv); |
| 988 | |
| 989 | ps3_vuart_irq_destroy(vuart_bus_priv.virq); |
| 990 | vuart_bus_priv.virq = NO_IRQ; |
| 991 | |
| 992 | kfree(vuart_bus_priv.bmp); |
| 993 | vuart_bus_priv.bmp = NULL; |
| 994 | |
| 995 | pr_debug(" <- %s:%d\n", __func__, __LINE__); |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | static int ps3_vuart_probe(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1000 | { |
| 1001 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1002 | struct ps3_vuart_port_driver *drv; |
| 1003 | struct ps3_vuart_port_priv *priv = NULL; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1004 | |
| 1005 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 1006 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1007 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
| 1008 | |
| 1009 | dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__, |
| 1010 | drv->core.core.name); |
| 1011 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1012 | BUG_ON(!drv); |
| 1013 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1014 | if (dev->port_number >= PORT_COUNT) { |
| 1015 | BUG(); |
| 1016 | return -EINVAL; |
| 1017 | } |
| 1018 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1019 | mutex_lock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1020 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1021 | result = ps3_vuart_bus_interrupt_get(); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1022 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1023 | if (result) |
| 1024 | goto fail_setup_interrupt; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1025 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1026 | if (vuart_bus_priv.devices[dev->port_number]) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1027 | dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__, |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1028 | __LINE__, dev->port_number); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1029 | result = -EBUSY; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1030 | goto fail_busy; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1033 | vuart_bus_priv.devices[dev->port_number] = dev; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1034 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1035 | /* Setup dev->driver_priv. */ |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1036 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1037 | dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv), |
| 1038 | GFP_KERNEL); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1039 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1040 | if (!dev->driver_priv) { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1041 | result = -ENOMEM; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1042 | goto fail_dev_malloc; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1045 | priv = to_port_priv(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1046 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1047 | INIT_LIST_HEAD(&priv->tx_list.head); |
| 1048 | spin_lock_init(&priv->tx_list.lock); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1049 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1050 | INIT_LIST_HEAD(&priv->rx_list.head); |
| 1051 | spin_lock_init(&priv->rx_list.lock); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1052 | |
Tejun Heo | 4a8bb7f | 2014-03-07 10:24:48 -0500 | [diff] [blame] | 1053 | INIT_WORK(&priv->rx_list.work.work, ps3_vuart_work); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1054 | priv->rx_list.work.trigger = 0; |
| 1055 | priv->rx_list.work.dev = dev; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1056 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1057 | /* clear stale pending interrupts */ |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1058 | |
| 1059 | ps3_vuart_clear_rx_bytes(dev, 0); |
| 1060 | |
| 1061 | ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1062 | |
| 1063 | ps3_vuart_set_triggers(dev, 1, 1); |
| 1064 | |
| 1065 | if (drv->probe) |
| 1066 | result = drv->probe(dev); |
| 1067 | else { |
| 1068 | result = 0; |
| 1069 | dev_info(&dev->core, "%s:%d: no probe method\n", __func__, |
| 1070 | __LINE__); |
| 1071 | } |
| 1072 | |
| 1073 | if (result) { |
| 1074 | dev_dbg(&dev->core, "%s:%d: drv->probe failed\n", |
| 1075 | __func__, __LINE__); |
| 1076 | goto fail_probe; |
| 1077 | } |
| 1078 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1079 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1080 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1081 | return result; |
| 1082 | |
| 1083 | fail_probe: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1084 | ps3_vuart_set_interrupt_mask(dev, 0); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1085 | kfree(dev->driver_priv); |
| 1086 | dev->driver_priv = NULL; |
| 1087 | fail_dev_malloc: |
| 1088 | vuart_bus_priv.devices[dev->port_number] = NULL; |
| 1089 | fail_busy: |
| 1090 | ps3_vuart_bus_interrupt_put(); |
| 1091 | fail_setup_interrupt: |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1092 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1093 | dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1094 | return result; |
| 1095 | } |
| 1096 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1097 | /** |
| 1098 | * ps3_vuart_cleanup - common cleanup helper. |
| 1099 | * @dev: The struct ps3_system_bus_device instance. |
| 1100 | * |
| 1101 | * Cleans interrupts and HV resources. Must be called with |
| 1102 | * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and |
| 1103 | * ps3_vuart_shutdown. After this call, polled reading will still work. |
| 1104 | */ |
| 1105 | |
| 1106 | static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1107 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1108 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 1109 | |
| 1110 | ps3_vuart_cancel_async(dev); |
| 1111 | ps3_vuart_set_interrupt_mask(dev, 0); |
| 1112 | ps3_vuart_bus_interrupt_put(); |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * ps3_vuart_remove - Completely clean the device instance. |
| 1118 | * @dev: The struct ps3_system_bus_device instance. |
| 1119 | * |
| 1120 | * Cleans all memory, interrupts and HV resources. After this call the |
| 1121 | * device can no longer be used. |
| 1122 | */ |
| 1123 | |
| 1124 | static int ps3_vuart_remove(struct ps3_system_bus_device *dev) |
| 1125 | { |
| 1126 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 1127 | struct ps3_vuart_port_driver *drv; |
| 1128 | |
| 1129 | BUG_ON(!dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1130 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1131 | mutex_lock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1132 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1133 | dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, |
| 1134 | dev->match_id); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1135 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1136 | if (!dev->core.driver) { |
| 1137 | dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, |
| 1138 | __LINE__); |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1139 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1140 | return 0; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1141 | } |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1142 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1143 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
| 1144 | |
| 1145 | BUG_ON(!drv); |
| 1146 | |
| 1147 | if (drv->remove) { |
| 1148 | drv->remove(dev); |
| 1149 | } else { |
| 1150 | dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__, |
| 1151 | __LINE__); |
| 1152 | BUG(); |
| 1153 | } |
| 1154 | |
| 1155 | ps3_vuart_cleanup(dev); |
| 1156 | |
| 1157 | vuart_bus_priv.devices[dev->port_number] = NULL; |
| 1158 | kfree(priv); |
| 1159 | priv = NULL; |
| 1160 | |
| 1161 | dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1162 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * ps3_vuart_shutdown - Cleans interrupts and HV resources. |
| 1168 | * @dev: The struct ps3_system_bus_device instance. |
| 1169 | * |
| 1170 | * Cleans interrupts and HV resources. After this call the |
| 1171 | * device can still be used in polling mode. This behavior required |
| 1172 | * by sys-manager to be able to complete the device power operation |
| 1173 | * sequence. |
| 1174 | */ |
| 1175 | |
| 1176 | static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev) |
| 1177 | { |
| 1178 | struct ps3_vuart_port_driver *drv; |
| 1179 | |
| 1180 | BUG_ON(!dev); |
| 1181 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1182 | mutex_lock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1183 | |
| 1184 | dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, |
| 1185 | dev->match_id); |
| 1186 | |
| 1187 | if (!dev->core.driver) { |
| 1188 | dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, |
| 1189 | __LINE__); |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1190 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
| 1195 | |
| 1196 | BUG_ON(!drv); |
| 1197 | |
| 1198 | if (drv->shutdown) |
| 1199 | drv->shutdown(dev); |
| 1200 | else if (drv->remove) { |
| 1201 | dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n", |
| 1202 | __func__, __LINE__); |
| 1203 | drv->remove(dev); |
| 1204 | } else { |
| 1205 | dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__, |
| 1206 | __LINE__); |
| 1207 | BUG(); |
| 1208 | } |
| 1209 | |
| 1210 | ps3_vuart_cleanup(dev); |
| 1211 | |
| 1212 | dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1213 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1214 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1215 | return 0; |
| 1216 | } |
| 1217 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1218 | static int __init ps3_vuart_bus_init(void) |
Geert Uytterhoeven | 5b8e8ee | 2007-02-12 00:55:15 -0800 | [diff] [blame] | 1219 | { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1220 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1221 | |
| 1222 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
Geert Uytterhoeven | ef596c6 | 2007-03-10 00:05:38 +0100 | [diff] [blame] | 1223 | return -ENODEV; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1224 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1225 | mutex_init(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1226 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1227 | return 0; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1230 | static void __exit ps3_vuart_bus_exit(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1231 | { |
| 1232 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1235 | core_initcall(ps3_vuart_bus_init); |
| 1236 | module_exit(ps3_vuart_bus_exit); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1237 | |
| 1238 | /** |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1239 | * ps3_vuart_port_driver_register - Add a vuart port device driver. |
| 1240 | */ |
| 1241 | |
| 1242 | int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv) |
| 1243 | { |
| 1244 | int result; |
| 1245 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1246 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); |
| 1247 | |
| 1248 | BUG_ON(!drv->core.match_id); |
| 1249 | BUG_ON(!drv->core.core.name); |
| 1250 | |
| 1251 | drv->core.probe = ps3_vuart_probe; |
| 1252 | drv->core.remove = ps3_vuart_remove; |
| 1253 | drv->core.shutdown = ps3_vuart_shutdown; |
| 1254 | |
| 1255 | result = ps3_system_bus_driver_register(&drv->core); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1256 | return result; |
| 1257 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1258 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register); |
| 1259 | |
| 1260 | /** |
| 1261 | * ps3_vuart_port_driver_unregister - Remove a vuart port device driver. |
| 1262 | */ |
| 1263 | |
| 1264 | void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv) |
| 1265 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1266 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); |
| 1267 | ps3_system_bus_driver_unregister(&drv->core); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1268 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1269 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister); |